0

Okey, I solved this when i was just about to post here. I was looking for a way to distinguish between something like event Action testEvt1; and event Action testEvt1 { add { } remove { }} with reflection, since both end up as a pair of add_testEvt1 remove_testEvt1 methods. Surprisingly, CompilerGeneratedAttribute does not help here.

The solution turned out to be to look for private backing filed with the same name as the event - the compiler will only generate one for simple events and won't let you have other fields with the same name.

As the code demonstrates:

class Program
    {
        static void Main(string[] args)
        {
            EventInfo evt1 = typeof(a).GetEvent("testEvt1");
            EventInfo evt2 = typeof(a).GetEvent("testEvt2");

            var evt1Attrib = Attribute.GetCustomAttribute(evt1, typeof(CompilerGeneratedAttribute));
            var evt2Attrib = Attribute.GetCustomAttribute(evt2, typeof(CompilerGeneratedAttribute));

            var evt1Backfield = typeof(a).GetField(evt1.Name,BindingFlags.NonPublic | BindingFlags.Instance);
            var evt2Backfield = typeof(a).GetField(evt2.Name, BindingFlags.NonPublic | BindingFlags.Instance);
        }
    }

    public class a
    {
        //private Action testEvt1;
        public event Action testEvt1;
        public event Action testEvt2
        {
            add { }
            remove { }
        }
    }

evt1Attrib and evt2Attrib both end up null. But the backing filed can only be found for the simple testEvt1, not testEvt2.

I decided to post this if anyone else happens to have same problem and maybe ask, does anyone know an easier way to distinguis testEvt1 from testEvt2 and why the compiler does not add CompilerGeneratedAttribute to add and remove methods for events? I'd like to know of a method that does not rely on backing field naming rules that may be changed in the future.

Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50
  • BTW, that's called a _field-like event_. – SLaks Jul 12 '13 at 16:35
  • It should be added that the provided solution depends on the undocumented implementation detail of the compiler. In theory, it's not guaranteed that the backing field will have the same name as the event. More info in this thread http://stackoverflow.com/questions/9847424/is-the-backing-field-of-a-compiler-generated-event-always-guaranteed-to-use-the – AlexD Jan 03 '14 at 12:27
  • Yes. I wrote the part "I'd like to know of a method that does not rely on backing field naming rules that may be changed in the future." with that in mind. Sorry, if it turned out somewhat obscure. – Ivan Koshelev Jan 04 '14 at 00:34

1 Answers1

0

Answer described above. You have to look for a private instance or static (same as event in question) backing field with the same name.

Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50
  • I'd suggest you to edit your question back to the original, and edit this post to the actual solution, and accepting it as answer. That way there is separation between the problem and solution, and the question will be marked as answered. – Ron Sijm Jul 20 '13 at 13:38