Sap*_*huA 8 c# dnx asp.net-core
我正在尝试检查属性是否具有属性.过去常常这样做:
Attribute.IsDefined(propertyInfo, typeof(AttributeClass));
Run Code Online (Sandbox Code Playgroud)
但是我得到一个警告,它在DNX Core 5.0中不可用(它仍然在DNX 4.5.1中).
它尚未实现还是像其他类型/反射相关的东西一样移动?
我正在使用beta7.
Hen*_*ema 11
编辑:包中
实际上似乎有一个IsDefined
扩展方法System.Reflection.Extensions
.用法:
var defined = propertyInfo.IsDefined(typeof(AttributeClass));
Run Code Online (Sandbox Code Playgroud)
您需要包含System.Reflection
命名空间.参考源代码可以在这里找到.除此之外MemberInfo
,它的工作原理上Assembly
,Module
和ParameterInfo
也.
看起来它还没有移植到.NET Core.同时您可以使用它GetCustomAttribute
来确定是否在属性上设置了属性:
bool defined = propertyInfo.GetCustomAttribute(typeof(AttributeClass)) != null;
Run Code Online (Sandbox Code Playgroud)
您可以将其烘焙为扩展方法:
public static class MemberInfoExtensions
{
public static bool IsAttributeDefined<TAttribute>(this MemberInfo memberInfo)
{
return memberInfo.IsAttributeDefined(typeof(TAttribute));
}
public static bool IsAttributeDefined(this MemberInfo memberInfo, Type attributeType)
{
return memberInfo.GetCustomAttribute(attributeType) != null;
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
bool defined = propertyInfo.IsAttributeDefined<AttributeClass>();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1834 次 |
最近记录: |