Event Reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BK

    Event Reflection

    Hi,

    I have a class which has a lot of events (>100). For some reasons, I have to
    go through all invocation lists to do something. What I'm wondering is that,
    is there any way to use reflection to get their InvocationList without going
    through each event?

    // tedious
    foreach (Delegate handler in Event1.GetInvoc ationList()) {...}
    foreach (Delegate handler in Event2.GetInvoc ationList()) {...}
    foreach (Delegate handler in Event3.GetInvoc ationList()) {...}
    ....
    foreach (Delegate handler in Event189.GetInv ocationList()) {...}

    // user reflection???
    .....

    Looks like this is a hard question as I haven't googled any answers. Thanks

    BK


  • Greg Ewing [MVP]

    #2
    Re: Event Reflection

    BK, I don't understand what you are trying to do. What do you want that
    GetInvocationLi st() doesn't give you?

    --
    Greg Ewing [MVP]
    Accomplish Your Mission with Better IT | IT support for nonprofits. You deserve peace of mind. National and remote outsourced nonprofit IT.


    "BK" <bluekiteNO-SPAM@rogers.com > wrote in message
    news:eGq6N8WeDH A.696@TK2MSFTNG P09.phx.gbl...[color=blue]
    > Hi,
    >
    > I have a class which has a lot of events (>100). For some reasons, I have[/color]
    to[color=blue]
    > go through all invocation lists to do something. What I'm wondering is[/color]
    that,[color=blue]
    > is there any way to use reflection to get their InvocationList without[/color]
    going[color=blue]
    > through each event?
    >
    > // tedious
    > foreach (Delegate handler in Event1.GetInvoc ationList()) {...}
    > foreach (Delegate handler in Event2.GetInvoc ationList()) {...}
    > foreach (Delegate handler in Event3.GetInvoc ationList()) {...}
    > ...
    > foreach (Delegate handler in Event189.GetInv ocationList()) {...}
    >
    > // user reflection???
    > ....
    >
    > Looks like this is a hard question as I haven't googled any answers.[/color]
    Thanks[color=blue]
    >
    > BK
    >
    >[/color]


    Comment

    • John Saunders

      #3
      Re: Event Reflection

      "BK" <bluekiteNO-SPAM@rogers.com > wrote in message
      news:eGq6N8WeDH A.696@TK2MSFTNG P09.phx.gbl...[color=blue]
      > Hi,
      >
      > I have a class which has a lot of events (>100). For some reasons, I have[/color]
      to[color=blue]
      > go through all invocation lists to do something. What I'm wondering is[/color]
      that,[color=blue]
      > is there any way to use reflection to get their InvocationList without[/color]
      going[color=blue]
      > through each event?[/color]

      Are you aware of the alternate way to declare events?

      public event EventHandler Something
      {
      add {add "value" to some list}
      remove {remove "value" from the list}
      }

      The System.Web.UI.C ontrol class defines its events this way. It also
      declares an "Events" property of type EventHandlerLis t. The EventHandlerLis t
      class defines AddHandler and RemoveHandler methods, and an indexer. These
      all take an arbitrary object as a parameter:

      private static readonly object SomeEvent = new object();

      public event EventHandler Something
      {
      add
      {
      Events.AddHandl er(SomeEvent, value);
      }
      remove
      {
      Events.RemoveHa ndler(SomeEvent , value);
      }
      }

      protected virtual void OnSomething(Eve ntArgs e)
      {
      EventHandler someEventDelega te = (EventHandler) Events[SomeEvent];
      if (someEventDeleg ate != null)
      someEventDelega te(this, e);
      }

      Now, it seems to me that you could do something similar, perhaps using a
      HashTable instead of an EventHandlerLis t. You would then be able to iterate
      over the hashtable and access each event handler in the loop.
      --
      John Saunders
      Internet Engineer
      john.saunders@s urfcontrol.com

      }


      Comment

      • Eric Gunnerson [MS]

        #4
        Re: Event Reflection

        Sure:

        Type type = typeof (YourClassName) ; // can use instance.GetTyp e()
        as well.

        foreach (MemberInfo memberInfo in type.GetMembers ())
        {
        if (memberInfo.Mem berType == MemberType.Even t)

        // and then you use MemberType to get the value here. It will probably
        come back as as a delegate (I'm not sure).
        }

        Hope that helps.



        --
        Eric Gunnerson

        Visit the C# product team at http://www.csharp.net
        Eric's blog is at http://blogs.gotdotnet.com/ericgu/

        This posting is provided "AS IS" with no warranties, and confers no rights.
        "BK" <bluekiteNO-SPAM@rogers.com > wrote in message
        news:eGq6N8WeDH A.696@TK2MSFTNG P09.phx.gbl...[color=blue]
        > Hi,
        >
        > I have a class which has a lot of events (>100). For some reasons, I have[/color]
        to[color=blue]
        > go through all invocation lists to do something. What I'm wondering is[/color]
        that,[color=blue]
        > is there any way to use reflection to get their InvocationList without[/color]
        going[color=blue]
        > through each event?
        >
        > // tedious
        > foreach (Delegate handler in Event1.GetInvoc ationList()) {...}
        > foreach (Delegate handler in Event2.GetInvoc ationList()) {...}
        > foreach (Delegate handler in Event3.GetInvoc ationList()) {...}
        > ...
        > foreach (Delegate handler in Event189.GetInv ocationList()) {...}
        >
        > // user reflection???
        > ....
        >
        > Looks like this is a hard question as I haven't googled any answers.[/color]
        Thanks[color=blue]
        >
        > BK
        >
        >[/color]


        Comment

        • BK

          #5
          Re: Event Reflection

          Yes, I tried this, but there is no way to get the value of event delegate.

          "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> wrote in message
          news:#nKaIileDH A.1836@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Sure:
          >
          > Type type = typeof (YourClassName) ; // can use[/color]
          instance.GetTyp e()[color=blue]
          > as well.
          >
          > foreach (MemberInfo memberInfo in type.GetMembers ())
          > {
          > if (memberInfo.Mem berType == MemberType.Even t)
          >
          > // and then you use MemberType to get the value here. It will probably
          > come back as as a delegate (I'm not sure).
          > }
          >
          > Hope that helps.
          >
          >
          >
          > --
          > Eric Gunnerson
          >
          > Visit the C# product team at http://www.csharp.net
          > Eric's blog is at http://blogs.gotdotnet.com/ericgu/
          >
          > This posting is provided "AS IS" with no warranties, and confers no[/color]
          rights.[color=blue]
          > "BK" <bluekiteNO-SPAM@rogers.com > wrote in message
          > news:eGq6N8WeDH A.696@TK2MSFTNG P09.phx.gbl...[color=green]
          > > Hi,
          > >
          > > I have a class which has a lot of events (>100). For some reasons, I[/color][/color]
          have[color=blue]
          > to[color=green]
          > > go through all invocation lists to do something. What I'm wondering is[/color]
          > that,[color=green]
          > > is there any way to use reflection to get their InvocationList without[/color]
          > going[color=green]
          > > through each event?
          > >
          > > // tedious
          > > foreach (Delegate handler in Event1.GetInvoc ationList()) {...}
          > > foreach (Delegate handler in Event2.GetInvoc ationList()) {...}
          > > foreach (Delegate handler in Event3.GetInvoc ationList()) {...}
          > > ...
          > > foreach (Delegate handler in Event189.GetInv ocationList()) {...}
          > >
          > > // user reflection???
          > > ....
          > >
          > > Looks like this is a hard question as I haven't googled any answers.[/color]
          > Thanks[color=green]
          > >
          > > BK
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • BK

            #6
            Re: Event Reflection

            Basically, I want to get the value of each event delegate using its name
            string, like "event1", instead of going through each event to do something
            the same, which is very tedious.

            Thanks,

            "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
            news:#XQX4SXeDH A.3788@tk2msftn gp13.phx.gbl...[color=blue]
            > BK, I don't understand what you are trying to do. What do you want that
            > GetInvocationLi st() doesn't give you?
            >
            > --
            > Greg Ewing [MVP]
            > http://www.citidc.com
            >
            > "BK" <bluekiteNO-SPAM@rogers.com > wrote in message
            > news:eGq6N8WeDH A.696@TK2MSFTNG P09.phx.gbl...[color=green]
            > > Hi,
            > >
            > > I have a class which has a lot of events (>100). For some reasons, I[/color][/color]
            have[color=blue]
            > to[color=green]
            > > go through all invocation lists to do something. What I'm wondering is[/color]
            > that,[color=green]
            > > is there any way to use reflection to get their InvocationList without[/color]
            > going[color=green]
            > > through each event?
            > >
            > > // tedious
            > > foreach (Delegate handler in Event1.GetInvoc ationList()) {...}
            > > foreach (Delegate handler in Event2.GetInvoc ationList()) {...}
            > > foreach (Delegate handler in Event3.GetInvoc ationList()) {...}
            > > ...
            > > foreach (Delegate handler in Event189.GetInv ocationList()) {...}
            > >
            > > // user reflection???
            > > ....
            > >
            > > Looks like this is a hard question as I haven't googled any answers.[/color]
            > Thanks[color=green]
            > >
            > > BK
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...