Results 1 to 11 of 11

Thread: [Unresolved - Help Please] Using Reflection with Eventing

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30

    Exclamation [Unresolved - Help Please] Using Reflection with Eventing

    Hi All,

    I've been nutting on this for a couple of days now and I'm going a little mad.

    I load an assemlby (Class1) using LoadFrom then reference its method (Test). When I try to use AddHandler to have the loaded method used by a control from my Original class (OrgClass) I seem to run into error after error.

    Has anyone got an example of how to do this that I would be able to learn from?

    Just to clarify, OrgClass dynamically loads Class1 then references its Test method. Then with a control in OrgClass I try to add a handler to its OnClick event.

    Any help would be greatly appreciated.

    Ciao,

    Ozki.
    Last edited by Ozki1976; Jan 7th, 2004 at 06:42 PM.
    <! Ozki !>

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What error are you getting? Does the signature match? Are you trying to attached the handler to the event instance or instance method of the object you created from the other assembly? Show your code and we can help.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    Hi Edneeis,

    The error I get is within the IDE itself. Following is the code then the error msg.

    Main Program

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Load an assembly at run-time containing Class1 with method Test
    Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("C:\Inetpub\wwwroot\DynamicLoader\DymanicLibrary\bin\DymanicLibr ary.dll")

    ' Create a button control at run-time
    Dim btn As New Web.UI.WebControls.Button()

    Dim t As Type
    Dim m As MethodInfo

    ' Get the type of Class1 from the dynamically loaded assembly
    t = assem.GetType("Class1")

    ' Get the method Test from Class1
    m = t.GetMethod("Test")

    ' Create the handler for btn.Click so that it executes Test when the event is fired
    AddHandler btn.Click, AddressOf m

    ' Initialize the btn control
    btn.Width = New Unit(100)
    btn.Height = New Unit(50)
    btn.Text = "Yo Mama"

    ' Add the btn control to a placeholder within the Page
    ph.Controls.Add(btn)
    End Sub

    Class1 is simply a basic class inheriting the Page class with:
    Public Sub Test (ByVal sender As System.Object, ByVal e As System.EventArgs)
    Response.Write("<P>Hi Y'All</P>")
    End Sub

    The error I receive within the IDE is:
    'AddressOf' operand must be the name of a method; no parentheses are needed.
    This is in relation to the underlined variable m in the code.

    Can you please help?

    Thanks,
    Last edited by Ozki1976; Jan 7th, 2004 at 06:37 PM.
    <! Ozki !>

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    Hi All,

    Can someone pleaaaase help?

    With the code I posted above, the AddHandler sub is what is causing me the problem. Does anyone know how I could dynamically load an assembly (assem) then attach a sub from that assembly (Test) to the event in the dynamically created control (btn)?
    The signatures for the event and the sub are the same.

    Many Thanks,
    <! Ozki !>

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The issue is compounded by the fact that it is an ASP.NET project not a windows project. I know how to do it in a windows project but not in a web project sorry. Maybe someone in the ASP.NET section will be able to help. Maybe it will work in a web form too but I had trouble once before in a web app with dynamic controls and events. It would drop my dynamic event handler on every refresh. Anyway the syntax of AddHandler is incorrect on the AddressOf part.

    VB Code:
    1. Dim btn As New Button
    2.             AddHandler btn.Click, AddressOf Test
    3.  
    4.             btn.Width = New Unit(100)
    5.             btn.Height = New Unit(50)
    6.             btn.Text = "Yo Mama"
    This assumes the current page is of type Class1 which inherits from Page.
    Last edited by Edneeis; Jan 7th, 2004 at 04:11 AM.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    Hi Edneeis,

    Thanks for the post but maybe I haven't explained my situation well. The sub Test comes from an assembly that I load in the code so using AddHandler btn.Click, AddressOf Test won't work as the IDE doesn't know what Test is since it's loaded dynamically, hence I get a compilation error.
    I understand the usage of AddHandler in relation to a sub that sits within the same class that AddHandler is being called but when loading a class dynamically at runtime I just can't seem to get the eventing to work as I would like.
    Any other thoughts would be greatly appreciated.

    Thanks,
    <! Ozki !>

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If sub test is the method of an object then you can simply refer to the instance of the object in question.
    VB Code:
    1. Dim obj As New Class1
    2.             Dim btn As New Button
    3.             'refer to instance variable
    4.             AddHandler btn.Click, AddressOf obj.Test
    5.             btn.Width = New Unit(100)
    6.             btn.Height = New Unit(50)
    7.             btn.Text = "Yo Mama"

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    Hi Edneeis,

    Thanks again for the post. Since Class1 is being loaded at run-time the compiler doesn't recognise it in code directly that is why I am using reflection. I've ammended the code above to show comments to clarify what I am trying to achieve.
    If you have any other thoughts on what I could do it would be greatly appreciated.

    Thanks again,
    <! Ozki !>

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well even loading the object at runtime is fine you just have to cast it to its strong type for the method. Also in your code you have t as a Type not an instance of Class1 so you wont be able to attach anything to that. You need an instance in order to have events to attach to. A Type is just a blueprint of an object but is not the object itself.
    VB Code:
    1. ' Load an assembly at run-time containing Class1 with method Test
    2. Dim assem As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("C:\Inetpub\wwwroot\DynamicLoader\DymanicLibrary\bin\DymanicLibrary.dll")
    3.  
    4. ' Create a button control at run-time
    5. Dim btn As New Web.UI.WebControls.Button()
    6.  
    7. ' Get the type of Class1 from the dynamically loaded assembly
    8. Dim obj As Object = Activator.CreateInstance(assem.GetType("Class1"))
    9.  
    10.  
    11. ' Create the handler for btn.Click so that it executes Test when the event is fired
    12. AddHandler btn.Click, AddressOf DirectCast(obj, Class1).Test
    13.  
    14. ' Initialize the btn control
    15. btn.Width = New Unit(100)
    16. btn.Height = New Unit(50)
    17. btn.Text = "Yo Mama"
    18.  
    19. ' Add the btn control to a placeholder within the Page
    20. ph.Controls.Add(btn)

    What exactly are you trying to accomplish with this? There is probably an easier way of doing it.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    Hi Edneeis,

    The problem again is that Class1 in the AddEvent statement you gave is not defined at design-time so the compiler doesn't recognize it.
    What I am trying to achieve is to be able to load any custom assembly that I write at run-time and have dynamically created controls use the methods from the loaded assembly in their events.
    Essentially this application that I am writing will lookup the required controls (from a db) for a Page then create the controls at run-time and lookup the associated assembly from a db and all associated events and link them to the controls created at run-time.
    I hope I explained that well

    Thanks again,
    <! Ozki !>

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Then you need to follow the same basic rules as a plugin system and have all of the classes that will be used either be derived from the same base class or implement the same interface. Then you can cast to that base type/interface to access the method/events.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width