c# - CallerMemberName and enums -
From this reply:
For this question:
You can use CallerMemberName to locate that property. Which is very good, unfortunately, it seems that it does not work with energy. For example:
public static zero main () {var mi = typeof (MyEnum) .GetMember ("Value1"); Var attr = mi [0] .GetCustomAttribute & lt; FooAttribute & gt; (); M = typef (bar) .GetMember ("SomeProp"); Attr = mi [0] .GetCustomAttribute & lt; FooAttribute & gt; (); } Public class bar {{Foo} public string SomeProp {get; Set; }} Public class FooAttribute: attribute {public FooAttribute ([CallerMemberName} string propName = null) {Console.WriteLine ("Propname =" + propName); }} Enum MyEnum {[Foo] Value1, [Foo] Value2}; propName when you use it for MyEnum is zero, but the class bar For this it works as expected (ie it is SomeProp ). Is there a way to do this or something like this? Or am I stuck with adding an asset to FooAttribute and when I add property to animate:
public class FooAttribute: attribute {public MyEnum AttachedTo {get ; Set; }} Enum MyEnum {[Foo (AttachedTo = MyEnum.Value1)] Value1, [Foo (AttachedTo = MyEnum.Value2)] Value2};
Which is tedious and there is a possibility of error prone.
callerMemberName assembly level or const Is not available because an enum value is essentially a const, you can not get that information. If you apply [Foo] to the public const member on class bar , you will see the same behavior.
As I was putting a test, my inner voice was shouting that I was doing something wrong due to the properties of Constracta normally it seems in opposition to the nature of decorators. I think that you are in the unknown, 'Here are the monsters,' area you want to reconsider your design and consider a different path.
Comments
Post a Comment