本文整理汇总了C#中System.Reflection.PropertyInfo.?.GetCustomAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.?.GetCustomAttributes方法的具体用法?C# PropertyInfo.?.GetCustomAttributes怎么用?C# PropertyInfo.?.GetCustomAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.?.GetCustomAttributes方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Get
/// <summary>
/// Get the raw configuration value from the source.
/// </summary>
/// <param name="valueName">The name of the value to get.</param>
/// <param name="property">If there is a property on the <see cref="Config"/> instance that matches the requested value name then this will contain the reference to that property.</param>
/// <param name="value">The value found in the source.</param>
/// <returns>True if the config value was found in the source, false otherwise.</returns>
public bool Get(string valueName, PropertyInfo property, out string value)
{
// Get the default value attribute
var attribute = property?.GetCustomAttributes(true)
.OfType<DefaultValueAttribute>()
.SingleOrDefault();
// If there is no attribute return false
if (attribute == null)
{
value = null;
return false;
}
// If the value if null handle that explicitly
if (attribute.Value == null)
{
value = null;
return true;
}
// Convert the value to a string and return true
value = attribute.Value as string ?? attribute.Value.ToString();
return true;
}
示例2: JsonName
/// <summary>
/// Returns the name specified by a JsonProperty attribute if it exists, otherwise the property name
/// </summary>
/// <param name="prop"></param>
/// <returns>The [JsonProperty] name of property if it exists, or the property name</returns>
public static string JsonName(PropertyInfo prop)
=> prop?.GetCustomAttributes<JsonPropertyAttribute>(true).Select(p => p.PropertyName).FirstOrDefault()
?? prop.Name;