本文整理汇总了C#中System.Reflection.PropertyInfo.GetCustomAttributesData方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetCustomAttributesData方法的具体用法?C# PropertyInfo.GetCustomAttributesData怎么用?C# PropertyInfo.GetCustomAttributesData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetCustomAttributesData方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDecoratedProperty
private static void CreateDecoratedProperty(TypeBuilder tb, PropertyInfo prop, Type decoratorType, string decoratedPropertyName, Type decoratedType)
{
var propertyName = prop.Name;
var propertyType = prop.PropertyType;
var propertyBuilder = tb.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
foreach (var builder in prop.GetCustomAttributesData().Select(GetAttributeCopy))
propertyBuilder.SetCustomAttribute(builder);
var getPropMthdBldr = tb.DefineMethod("get_" + propertyName,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType,
Type.EmptyTypes);
var getIl = getPropMthdBldr.GetILGenerator();
getIl.Emit(OpCodes.Ldarg_0);
getIl.Emit(OpCodes.Castclass, decoratorType);
getIl.Emit(OpCodes.Callvirt, decoratorType.GetProperty(decoratedPropertyName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetGetMethod(true));
getIl.Emit(OpCodes.Callvirt, decoratedType.GetProperty(propertyName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetGetMethod(true));
getIl.Emit(OpCodes.Ret);
var setPropMthdBldr = tb.DefineMethod("set_" + propertyName,
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null,
new[] { propertyType });
propertyBuilder.SetGetMethod(getPropMthdBldr);
var setIl = setPropMthdBldr.GetILGenerator();
//var modifyProperty = setIl.DefineLabel();
//var exitSet = setIl.DefineLabel();
//setIl.MarkLabel(modifyProperty);
setIl.Emit(OpCodes.Ldarg_0);
//setIl.Emit(OpCodes.Castclass, decoratorType);
setIl.Emit(OpCodes.Callvirt, decoratorType.GetProperty(decoratedPropertyName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetGetMethod(true));
setIl.Emit(OpCodes.Ldarg_1);
setIl.Emit(OpCodes.Callvirt, decoratedType.GetProperty(propertyName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).GetSetMethod(true));
//setIl.Emit(OpCodes.Nop);
//setIl.MarkLabel(exitSet);
setIl.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(setPropMthdBldr);
}
示例2: 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(")");
}
}
示例3: EnrichProperty
public void EnrichProperty(IProcessingContext context, PropertyInfo propertyInfo)
{
GenerateAttributeElements(context, propertyInfo.GetCustomAttributesData());
}
示例4: GetPropertyAttribute
private ValdrAttribute GetPropertyAttribute(PropertyInfo prop, string attributeName)
{
foreach (var data in prop.GetCustomAttributesData())
{
if (data.AttributeType.Name == attributeName)
{
var attr = new ValdrAttribute();
if (attributeName == nameof(StringLengthAttribute) && data.ConstructorArguments.Count > 0)
{
attr.Maximum = (int)data.ConstructorArguments[0].Value;
}
else if (attributeName == nameof(RangeAttribute) && data.ConstructorArguments.Count > 1)
{
attr.Minimum = (int)data.ConstructorArguments[0].Value;
attr.Maximum = (int)data.ConstructorArguments[1].Value;
}
else if (attributeName == nameof(RegularExpressionAttribute) && data.ConstructorArguments.Count > 0)
{
attr.Pattern = (string)data.ConstructorArguments[0].Value;
}
if (data.NamedArguments != null)
{
foreach (var item in data.NamedArguments)
{
GetAttribute(attr, item.MemberName, item.TypedValue.Value);
}
}
return attr;
}
}
return null;
}
示例5: GetUsedTypes
/// <summary>
/// Gets the used namespaces in the property, including the property type
/// and also all of its custom attributes and attribute values.
/// </summary>
private static IEnumerable<string> GetUsedTypes(PropertyInfo property)
{
Guard.NotNull(() => property, property);
var usedTypes = new[] { property.PropertyType.FullName }.AsEnumerable();
var customAttributes = property.GetCustomAttributesData();
// Add the namespaces of all attribute types
usedTypes = usedTypes.Concat(customAttributes
.Select(data => data.Constructor.DeclaringType.FullName));
// Add the namespaces of all the parameter of all constructors
usedTypes = usedTypes.Concat(customAttributes
.SelectMany(data => data.ConstructorArguments)
.Select(arg => arg.ArgumentType.FullName));
// Add the namespaces of all the named parameters
usedTypes = usedTypes.Concat(customAttributes
.SelectMany(data => data.NamedArguments)
.Select(arg => arg.TypedValue.ArgumentType.FullName));
// Add the namespaces of all constructor arguments that are typeof(..)
usedTypes = usedTypes.Concat(
from attr in customAttributes
from arg in attr.ConstructorArguments
where arg.ArgumentType == typeof(Type)
let type = (Type)arg.Value
select type.FullName);
// Add the namespaces of all named arguments that are typeof(..)
usedTypes = usedTypes.Concat(
from attr in customAttributes
from arg in attr.NamedArguments
where arg.TypedValue.ArgumentType == typeof(Type)
let type = (Type)arg.TypedValue.Value
select type.FullName);
return usedTypes;
}