本文整理汇总了C#中System.Reflection.PropertyInfo.GetAccessors方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetAccessors方法的具体用法?C# PropertyInfo.GetAccessors怎么用?C# PropertyInfo.GetAccessors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetAccessors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSetAccessorParameter
private ParameterInfo GetSetAccessorParameter(PropertyInfo pi)
{
return pi
.GetAccessors()
.First()
.GetParameters()
.First();
}
示例2: HasPublicProperties
//Check for public methods
public bool HasPublicProperties(PropertyInfo property)
{
if (property == null)
{
throw new NullReferenceException("Must supply the property parameter");
}
foreach (MethodInfo info in property.GetAccessors(true))
{
if (info.IsPublic == false)
return true;
}
return false;
}
示例3: IsPublicProperty
/// <summary>
/// Determines whether the specified property is public.
/// </summary>
/// <param name="pi">The property.</param>
/// <returns>
/// <c>true</c> if the specified property is public; otherwise, <c>false</c>.
/// </returns>
public static bool IsPublicProperty(PropertyInfo pi)
{
foreach(var m in pi.GetAccessors())
if(m.IsPublic)
return true;
return false;
}
示例4: GetPropertyModel
FieldModel GetPropertyModel(PropertyInfo propertyInfo) {
if (propertyInfo.DeclaringType != Type) return null;
TypeDesc typeDesc = ModelScope.TypeScope.GetTypeDesc(propertyInfo.PropertyType, propertyInfo);
if (!propertyInfo.CanRead) return null;
foreach (MethodInfo accessor in propertyInfo.GetAccessors())
if ((accessor.Attributes & MethodAttributes.HasSecurity) != 0)
throw new InvalidOperationException(Res.GetString(Res.XmlPropertyHasSecurityAttributes, propertyInfo.Name, Type.FullName));
if ((propertyInfo.DeclaringType.Attributes & TypeAttributes.HasSecurity) != 0)
throw new InvalidOperationException(Res.GetString(Res.XmlPropertyHasSecurityAttributes, propertyInfo.Name, Type.FullName));
MethodInfo getMethod = propertyInfo.GetGetMethod();
ParameterInfo[] parameters = getMethod.GetParameters();
if (parameters.Length > 0) return null;
return new FieldModel(propertyInfo, propertyInfo.PropertyType, typeDesc);
}
示例5: GetPropertyContractValue
private string GetPropertyContractValue(PropertyInfo property)
{
return GetMethodContractValue(property.GetAccessors(true)[0]);
}
示例6: GetPropertyDescription
protected override string GetPropertyDescription (PropertyInfo property, object instance)
{
StringBuilder sb = new StringBuilder ();
AddAttributes (sb, property);
AddMethodQualifiers (sb, property.GetAccessors(true)[0]);
string getter = null;
if (property.CanRead) {
string v = null;
try {
v = string.Format (" /* = {0} */", GetValue (property.GetValue (instance, null)));
}
catch {
}
getter = string.Format (PropertyGetFormat, v);
}
string setter = null;
if (property.CanWrite)
setter = PropertySet;
sb.AppendFormat (PropertyFormat, property.PropertyType, property.Name, getter, setter);
return sb.ToString();
}
示例7: CreatePropertyOverride
private static CodeMemberProperty CreatePropertyOverride(string structType, PropertyInfo pi,
CodeMethodReferenceExpression changeMethod)
{
CodeMemberProperty mp = new CodeMemberProperty();
mp.Name = pi.Name;
mp.Attributes = MemberAttributes.Override |
(pi.GetAccessors(true)[0].IsPublic ? MemberAttributes.Public : MemberAttributes.Family);
mp.Type = new CodeTypeReference(pi.PropertyType);
mp.HasGet = mp.HasSet = true;
mp.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression() {
TargetObject = new CodePropertyReferenceExpression() {
TargetObject = new CodeFieldReferenceExpression() {
TargetObject = new CodeThisReferenceExpression(),
FieldName = ShieldedField,
},
PropertyName = ShieldedValueProperty,
},
FieldName = pi.Name,
}));
// call base setter first, so they can see the old value (using getter) and new (using value)
mp.SetStatements.Add(new CodeAssignStatement(
new CodePropertyReferenceExpression() {
TargetObject = new CodeBaseReferenceExpression(),
PropertyName = pi.Name,
}, new CodePropertySetValueReferenceExpression()));
// using Modify has a huge advantage - if a class is big, we don't want to be passing
// copies of the underlying struct around.
mp.SetStatements.Add(new CodeSnippetStatement(
string.Format("{0}.Modify((ref {1} a) => [email protected]{2} = value);",
ShieldedField, structType, pi.Name)));
if (changeMethod != null)
mp.SetStatements.Add(new CodeMethodInvokeExpression(
changeMethod, new CodePrimitiveExpression(pi.Name)));
return mp;
}
示例8: AppendPropertyInfo
static private void AppendPropertyInfo(PropertyInfo property, StringBuilder sb)
{
sb.Append(".property ");
foreach (var attribute in property.GetCustomAttributesData())
{
AppendCustomAttributeData(attribute, sb);
sb.Append(" ");
}
foreach (var modreq in property.GetRequiredCustomModifiers())
{
sb.Append("modreq(");
AppendType(modreq, sb);
sb.Append(") ");
}
foreach (var modopt in property.GetOptionalCustomModifiers())
{
sb.Append("modopt(");
AppendType(modopt, sb);
sb.Append(") ");
}
if (property.CanRead && property.CanWrite)
{
sb.Append("readwrite ");
}
else if (property.CanRead)
{
sb.Append("readonly ");
}
else if (property.CanWrite)
{
sb.Append("writeonly ");
}
if (property.Attributes.HasFlag(PropertyAttributes.SpecialName)) sb.Append("specialname ");
if (property.Attributes.HasFlag(PropertyAttributes.RTSpecialName)) sb.Append("rtspecialname ");
var propertyAccesors = property.GetAccessors();
if (propertyAccesors.Length > 0)
{
sb.Append(propertyAccesors[0].IsStatic ? "static " : "instance ");
}
AppendType(property.PropertyType, sb);
sb.Append(" ");
sb.Append(property.Name);
var indexParameters = property.GetIndexParameters();
if (indexParameters.Length > 0)
{
sb.Append("(");
foreach (var indexParameter in indexParameters)
{
AppendParameterInfo(indexParameter, sb);
AppendComma(sb);
}
RemoveTrailingComma(sb);
sb.Append(")");
}
}
示例9: GetPropertyAttributes
/// <summary>
/// Determines lowest accessibility of all property accessors and other member attributes.
/// </summary>
public static PhpMemberAttributes GetPropertyAttributes(PropertyInfo/*!*/info)
{
var accessors = info.GetAccessors(true);
PhpMemberAttributes attributes = PhpMemberAttributes.None;
// find lowest visibility in all property accessors:
for (int i = 0; i < accessors.Length; i++)
{
if (accessors[i].IsStatic)
attributes |= PhpMemberAttributes.Static;
if (accessors[i].IsPrivate)
attributes |= PhpMemberAttributes.Private;
else if (accessors[i].IsFamily)
attributes |= PhpMemberAttributes.Protected;
//else if (accessors[i].IsPublic)
// visibility |= PhpMemberAttributes.Public;
}
return attributes;
}
示例10: PropertyDetail
public PropertyDetail(RootDetail parent, PropertyInfo pi)
: base(parent, pi)
{
_name = pi.Name;
_category = "property";
MethodInfo[] methods = pi.GetAccessors(true);
foreach (MethodInfo mi in methods)
{
MethodDetail m = new MethodDetail(this, mi);
if ((m.Name.Length > 3) && (mi.IsSpecialName))
{
m.Name = m.Name.Substring(0, 3);
}
m.Declaration = null;
_children.Add(m);
}
if (pi.GetIndexParameters().Length > 0)
{
CodeStringBuilder csbParameters = new CodeStringBuilder(AppendMode.Text);
foreach (ParameterInfo ip in pi.GetIndexParameters())
{
csbParameters.AppendParameterType(ip);
csbParameters.AppendText(", ");
_parameterCount++;
}
csbParameters.RemoveCharsFromEnd(2);
_parameterTypesList = csbParameters.ToString();
}
_visibility = VisibilityUtil.GetMostVisible(FilterChildren<MethodDetail>());
CodeStringBuilder csb = new CodeStringBuilder();
AppendAttributesDeclaration(csb);
csb.Mode = AppendMode.Html;
csb.AppendVisibility(_visibility);
csb.AppendText(" ");
csb.Mode = AppendMode.Both;
csb.AppendType(pi.PropertyType);
csb.AppendText(" ");
csb.AppendText(pi.Name);
if (this.ParameterCount > 0)
{
csb.AppendText("[");
csb.AppendText(this.ParameterTypesList);
csb.AppendText("]");
}
csb.Mode = AppendMode.Html;
csb.AppendNewline();
csb.AppendText("{");
csb.AppendNewline();
csb.AppendIndent();
foreach (MethodDetail mi in FilterChildren<MethodDetail>())
{
if (mi.Visibility != _visibility)
{
csb.AppendVisibility(mi.Visibility);
csb.AppendText(" ");
}
csb.AppendText(mi.Name);
csb.AppendText("; ");
}
csb.AppendNewline();
csb.AppendText("}");
_declaration = csb.ToString();
_declarationHtml = csb.ToHtmlString();
}
示例11: AppendPropertySignature
public override void AppendPropertySignature(StringBuilder sb, PropertyInfo pi)
{
MethodInfo getter = pi.GetGetMethod();
if (getter != null)
{
sb.Append( indentPrefix );
AppendMethodVisibility( sb, getter );
AppendMethodReturnType( sb, getter, defaultAbbreviation );
if ((pi.Name.Equals("Item") || pi.Name.Equals("Items"))
&& getter.GetParameters().Length != 0)
{
sb.Append("this");
AppendIndexParameters( sb, getter, defaultAbbreviation );
}
else
{
sb.Append( pi.Name );
}
sb.Append( " {" );
MethodInfo[] mi = pi.GetAccessors();
String accessors = "";
for (int i = 0; i < mi.Length; i++)
{
String modifiers = (IsVirtual(mi[i])) ? " virtual" : "";
if (IsPropertySet(mi[i]))
accessors = accessors + modifiers + " set;";
else
accessors = modifiers + " get;" + accessors;
}
sb.Append( accessors + " }" );
sb.Append(lineSeparator);
}
}
示例12: WriteProperty
private static void WriteProperty(string memberName, PropertyInfo propInfo)
{
Console.WriteLine("\t\tpublic {0} {1}", GetCSharpTypeName(propInfo.PropertyType), propInfo.Name);
Console.WriteLine("\t\t{");
var accessors = propInfo.GetAccessors();
if (accessors.Count(x => x.Name.StartsWith("get_")) > 0)
{
Console.WriteLine("\t\t\tget {{ return {0}.{1}; }}", memberName, propInfo.Name);
}
if (accessors.Count(x => x.Name.StartsWith("set_")) > 0)
{
Console.WriteLine("\t\t\tset {{ {0}.{1} = value; }}", memberName, propInfo.Name);
}
Console.WriteLine("\t\t}");
}
示例13: HasSetter
/// <summary>
/// Determines whether a given property has a setter accessor.
/// </summary>
/// <param name="property">The property to check.</param>
/// <returns>True if the property has a setter accessor, otherwise false.</returns>
private static bool HasSetter(PropertyInfo property)
{
MethodInfo[] methods;
bool result;
result = false;
methods = property.GetAccessors(false);
foreach (MethodInfo info in methods)
{
if (info.Name.StartsWith("set_"))
{
result = true;
break;
}
}
return result;
}
示例14: VerifyAddMethodsResult
private void VerifyAddMethodsResult(PropertyInfo myProperty, MethodBuilder expectedMethod)
{
MethodInfo[] actualMethods = myProperty.GetAccessors(true);
Assert.Equal(1, actualMethods.Length);
Assert.Equal(expectedMethod.Name, actualMethods[0].Name);
}
示例15: IsInteresting
internal static bool IsInteresting(PropertyInfo pi)
{
if (!pi.CanRead || !pi.CanWrite)
return false;
var access = pi.GetAccessors(true);
// must have both accessors, both virtual, and both either public or protected, no mixing.
if (access == null || access.Length != 2 || !access[0].IsVirtual)
return false;
if (access[0].IsPublic && access[1].IsFamily || access[0].IsFamily && access[1].IsPublic)
throw new InvalidOperationException("Virtual property accessors must both be public, or both protected.");
return access[0].IsPublic && access[1].IsPublic || access[0].IsFamily && access[1].IsFamily;
}