|
-
Jan 3rd, 2004, 08:31 AM
#1
Thread Starter
Junior Member
[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 !>
-
Jan 3rd, 2004, 02:50 PM
#2
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.
-
Jan 4th, 2004, 05:26 AM
#3
Thread Starter
Junior Member
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 !>
-
Jan 7th, 2004, 02:00 AM
#4
Thread Starter
Junior Member
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,
-
Jan 7th, 2004, 02:10 AM
#5
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:
Dim btn As New Button
AddHandler btn.Click, AddressOf Test
btn.Width = New Unit(100)
btn.Height = New Unit(50)
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.
-
Jan 7th, 2004, 06:03 AM
#6
Thread Starter
Junior Member
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,
-
Jan 7th, 2004, 11:08 AM
#7
If sub test is the method of an object then you can simply refer to the instance of the object in question.
VB Code:
Dim obj As New Class1
Dim btn As New Button
'refer to instance variable
AddHandler btn.Click, AddressOf obj.Test
btn.Width = New Unit(100)
btn.Height = New Unit(50)
btn.Text = "Yo Mama"
-
Jan 7th, 2004, 06:41 PM
#8
Thread Starter
Junior Member
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,
-
Jan 7th, 2004, 06:56 PM
#9
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:
' 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\DymanicLibrary.dll")
' Create a button control at run-time
Dim btn As New Web.UI.WebControls.Button()
' Get the type of Class1 from the dynamically loaded assembly
Dim obj As Object = Activator.CreateInstance(assem.GetType("Class1"))
' Create the handler for btn.Click so that it executes Test when the event is fired
AddHandler btn.Click, AddressOf DirectCast(obj, Class1).Test
' 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)
What exactly are you trying to accomplish with this? There is probably an easier way of doing it.
-
Jan 7th, 2004, 07:36 PM
#10
Thread Starter
Junior Member
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,
-
Jan 7th, 2004, 07:54 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|