c# - Get Attribute from object -
I have to get a specific attribute
on one of the properties of the object.
You then call:
var attr = someobject.GetMember (x = & gt; x.Height) .GetAttribute & Lt; FooAttribute & gt; ();
But I would like it in a clean phase like this:
var attr = someobject.GetAttribute & lt; FooAttribute & gt; (X = & gt; x height);
How do I add those two functions to give this signature?
Update: Besides, why does not it work for enums?
You will not be able to get that exact signature. For the method of working, three common type arguments are required (for object type T
, for attribute type TAttribute
, and for a property type TProperty
). Using code can be estimated using T code and TProperty , but unfortunately, once you have a general Specify type logic, you need to specify all of them.
Public Static Class Object Extension {Public Fixed TATT GetAttribute & lt; T, TAttribute, TProperty & gt; (This T example, expression & lt; Func & lt; T, TProperty & gt; & gt; Selector) where feature: attribute {var member = selector. As a Member Member; If (member! = Null) {Member of return. Mail Gate Custom Property (typef (TATTI)) As the first orphalt () TATAD; } Return tap; }}
This is the reason that the questions were different for starting in two ways. Writing is easy
var attr = someobject.GetMember (x = & gt; x.Height) .GetAttribute & lt; FooAttribute & gt; ();
Expect to write
var attr = someobject.GetAttribute & lt; Foo, FooAttribute, FooProperty & gt; (X = & gt; x.Height);
Comments
Post a Comment