c# - Where did Attribute.IsDefined go in DNX Core 5.0? -
i'm trying check if property has attribute. used done with:
attribute.isdefined(propertyinfo, typeof(attributeclass)); however warning it's not available in dnx core 5.0 (it still in dnx 4.5.1).
has not been implemented yet or has moved other type/reflection related stuff?
i'm using beta7.
edit:
there seems isdefined extension method in system.reflection.extensions package. usage:
var defined = propertyinfo.isdefined(typeof(attributeclass)); you need include system.reflection namespace. reference source code can found here. beside memberinfo, works on assembly, module , parameterinfo too.
this possibly faster using getcustomattribute.
original post:
looks it's not ported .net core yet. in mean while can use getcustomattribute determine whether attribute set on property:
bool defined = propertyinfo.getcustomattribute(typeof(attributeclass)) != null; you bake extension method:
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; } } and use this:
bool defined = propertyinfo.isattributedefined<attributeclass>();
Comments
Post a Comment