本文整理汇总了C#中System.Reflection.FieldInfo.GetCustomAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# FieldInfo.GetCustomAttribute方法的具体用法?C# FieldInfo.GetCustomAttribute怎么用?C# FieldInfo.GetCustomAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.FieldInfo
的用法示例。
在下文中一共展示了FieldInfo.GetCustomAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFunction
public static Functions.Functions GetFunction(FieldInfo field)
{
if (field.GetCustomAttribute<OkButtonAttribute>(false) != null)
return Functions.Functions.Ok;
if (field.GetCustomAttribute<CloseButtonAttribute>(false) != null)
return Functions.Functions.Close;
if (field.GetCustomAttribute<CancelButtonAttribute>(false) != null)
return Functions.Functions.Cancel;
return Functions.Functions.None;
}
示例2: GetAttributesTextFor
public override IEnumerable<string> GetAttributesTextFor(FieldInfo field, Usage defaultUsage, ParsingPolicyAttribute[] parsingPolicies)
{
var res = new List<string>(base.GetAttributesTextFor(field, defaultUsage, parsingPolicies));
var fieldType = field.FieldType;
var renameRule = field.GetCustomAttribute<RenameAttribute>();
string fieldName = field.GetCustomAttribute<NameAttribute>()?.Name ?? field.Name;
if (!field.IsDefined<RefAttribute>())
{
if (field.IsPolymorphic())
{
Type attributeType = !fieldType.IsArray || field.IsDefined<InlineAttribute>() ? typeof(XmlElementAttribute) : typeof(XmlArrayItemAttribute);
foreach (var t in field.GetKnownSerializableTypes())
res.Add(GetItemAttributeText(attributeType, t, renameRule));
}
else if (
field.FieldType.IsArray &&
!field.IsDefined<ConverterAttribute>() &&
!field.IsDefined<ParserAttribute>() &&
!parsingPolicies.Any(p => p.CanParse(field.FieldType)))
{
Type attributeType = field.IsDefined<InlineAttribute>() ? typeof(XmlElementAttribute) : typeof(XmlArrayItemAttribute);
Type itemTypeName = field.FieldType.GetElementType();
res.Add(GetItemAttributeText(attributeType, itemTypeName, renameRule));
}
}
var rawFieldType = field.GetRawFieldType(parsingPolicies);
if (rawFieldType.IsSimple())
{
res.Add(AttributeBuilder.GetTextFor<XmlAttributeAttribute>(fieldName));
}
else if (!res.Any(a => a.Contains(nameof(XmlElementAttribute))))
{
if (rawFieldType.IsArray)
res.Add(AttributeBuilder.GetTextFor<XmlArrayAttribute>(fieldName));
else
res.Add(AttributeBuilder.GetTextFor<XmlElementAttribute>(fieldName));
}
if (field.IsDefined<HiddenAttribute>())
res.Add(AttributeBuilder.GetTextFor<XmlIgnoreAttribute>());
return res.Where(a => a != null);
}
示例3: BuildConfigurationKey
private string BuildConfigurationKey(FieldInfo constant, string variableName)
{
// Can make this polymorphic if the need arises
if (constant.GetCustomAttribute<StaticKey>() != null)
{
return BuildStaticConfigurationKey(variableName);
}
return BuildEnvironmentConfigurationKey(variableName);
}
示例4: Add
internal void Add(FieldInfo fi)
{
if (fi.IsInitOnly)
return;
if (fi.IsPrivate)
{ // require DataMemberAttribute
if (fi.GetCustomAttribute<DataMemberAttribute>() == null)
return;
}
if (fi.GetCustomAttribute<IgnoreDataMemberAttribute>() != null)
return;
var right = makeFieldReader(new FieldFunctionInfo(fi));
if (right == null)
return;
var left = Expression.Field(_pResult, fi);
_bodyList.Add(Expression.Assign(left, right));
}
示例5: Visit
public void Visit(string settingsNamespace, string fieldPath, FieldInfo rawSettingsField, object rawSettings)
{
var defaultAttr = rawSettingsField.GetCustomAttribute<DefaultAttribute>();
if (defaultAttr != null)
{
if (!CanAssign(rawSettingsField, defaultAttr.Value))
throw new ConfigurationException("Invalid default field value for {0}.{1}: '{2}'.", rawSettingsField.DeclaringType.Name, rawSettingsField.Name, defaultAttr.Value);
object targetValue = rawSettingsField.GetValue(rawSettings);
if (targetValue == null)
{
if (rawSettingsField.FieldType == typeof(string)) rawSettingsField.SetValue(rawSettings, defaultAttr.Value.ToString());
else rawSettingsField.SetValue(rawSettings, defaultAttr.Value);
}
}
}
示例6: ToString
static string ToString(FieldInfo sourceField, object container)
{
object value = sourceField.GetValue(container);
// Handle nullable types here
if (value != null && Nullable.GetUnderlyingType(sourceField.FieldType) != null)
{
bool hasValue = (bool)sourceField.FieldType.GetProperty("HasValue").GetValue(value);
if (hasValue) value = sourceField.FieldType.GetProperty("Value").GetValue(value);
}
ConverterAttribute converter = sourceField.GetCustomAttribute<ConverterAttribute>();
if (converter != null)
return converter.ToString(value, sourceField);
else
return value?.ToString();
}
示例7: CreateValueDescriptor
private static ValueDescriptor CreateValueDescriptor(FieldInfo enumValue)
{
Debug.Assert(enumValue != null, "enumValue");
string displayName = null;
string description = null;
var displayAttribute = enumValue.GetCustomAttribute<DisplayAttribute>();
if (displayAttribute != null)
{
displayName = displayAttribute.Name;
description = displayAttribute.Description;
}
displayName = displayName ?? enumValue.Name;
return new ValueDescriptor(displayName, description);
}
示例8: TsProperty
/// <summary>
/// Initializes a new instance of the TsProperty class with the specific CLR field.
/// </summary>
/// <param name="memberInfo">The CLR field represented by this instance of the TsProperty.</param>
public TsProperty(FieldInfo memberInfo)
{
this.MemberInfo = memberInfo;
this.Name = memberInfo.Name;
if (memberInfo.ReflectedType.IsGenericType) {
var definitionType = memberInfo.ReflectedType.GetGenericTypeDefinition();
var definitionTypeProperty = definitionType.GetProperty(memberInfo.Name);
if (definitionTypeProperty.PropertyType.IsGenericParameter) {
this.PropertyType = TsType.Any;
} else {
this.PropertyType = memberInfo.FieldType.IsEnum ? new TsEnum(memberInfo.FieldType) : new TsType(memberInfo.FieldType);
}
} else {
var propertyType = memberInfo.FieldType;
if (propertyType.IsNullable()) {
propertyType = propertyType.GetNullableValueType();
}
this.PropertyType = propertyType.IsEnum ? new TsEnum(propertyType) : new TsType(propertyType);
}
var attribute = memberInfo.GetCustomAttribute<TsPropertyAttribute>(false);
if (attribute != null) {
if (!string.IsNullOrEmpty(attribute.Name)) {
this.Name = attribute.Name;
}
this.IsOptional = attribute.IsOptional;
}
this.IsIgnored = (memberInfo.GetCustomAttribute<TsIgnoreAttribute>(false) != null);
if (memberInfo.IsLiteral && !memberInfo.IsInitOnly) {
// it's a constant
this.ConstantValue = memberInfo.GetValue(null);
} else {
// not a constant
this.ConstantValue = null;
}
}
示例9: GetHelpText
public static void GetHelpText(FieldInfo field, ref string helpText)
{
HelpTextAttribute attr = field.GetCustomAttribute<HelpTextAttribute>();
if(attr != null)
helpText = attr.HelpText;
}
示例10: GetDisplayName
public static void GetDisplayName(FieldInfo field, ref string displayName)
{
DisplayNameAttribute attr = field.GetCustomAttribute<DisplayNameAttribute>();
if(attr != null)
displayName = attr.DisplayName;
}
示例11: HDRColor
public static bool HDRColor(FieldInfo field)
{
HDRAttribute attr = field.GetCustomAttribute<HDRAttribute>();
if(attr != null)
return attr.HDR;
else
return false;
}
示例12: UseFieldAsShaderConstant
public static bool UseFieldAsShaderConstant(FieldInfo field)
{
UseAsShaderConstantAttribute attr = field.GetCustomAttribute<UseAsShaderConstantAttribute>();
if(attr != null)
return attr.UseAsShaderConstant;
else
return true;
}
示例13: GetStepSize
public static void GetStepSize(FieldInfo field, ref int stepSize)
{
StepSizeAttribute attr = field.GetCustomAttribute<StepSizeAttribute>();
if(attr != null)
stepSize = attr.StepSizeInt;
}
示例14: GetMaxValue
public static void GetMaxValue(FieldInfo field, ref int maxValue)
{
MaxValueAttribute attr = field.GetCustomAttribute<MaxValueAttribute>();
if(attr != null)
maxValue = attr.MaxValueInt;
}
示例15: NotUndoableField
private static bool NotUndoableField(FieldInfo field)
{
#if NETFX_CORE
var attr = field.GetCustomAttribute(typeof(NotUndoableAttribute));
return (attr != null);
#else
return Attribute.IsDefined(field, typeof(NotUndoableAttribute));
#endif
}