APaule Posted May 23, 2005 Posted May 23, 2005 Thanks to everyone again you are brilliant. davearia is right. You are all brilliant, and even though self-praise stinks, I am brilliant too :D , BECAUSE: What puzzled me all the time (beside the solution for davearia) was the question, how to create random controls out of the types of the System.Windows.Forms namespace that come similar to davearia's question, for instance out of a table? So all you have is strings. And possibly with Option Strict On. And here is what I elaborated (if sombody is interested), owing to all the hints above: Dim Asm As [Assembly] Dim vType As Type Dim Ctrl As Control Asm = [Assembly].LoadWithPartialName("System.Windows.Forms") vType = Asm.GetType("System.Windows.Forms.TextBox") Ctrl = CType(Activator.CreateInstance(vType), Control) Ctrl.Name = "myTextBox" Me.Controls.Add(Ctrl) The one and only question left is, how to add an eventhandler to a control, without hardcoding the delegates. I found no method in the reflection namespace that returns an object that could be casted into an eventhandler. But maybe sombody else knows and let us know. I'd appreciate it. :) Quote
Leaders snarfblam Posted May 23, 2005 Leaders Posted May 23, 2005 There is System.Reflection.EventInfo class. The System.Type class has a method, GetEvents(), which returns an array of EventInfo objects representing each of a certain type's events. How to handle these events without a preexisting delegate is beyond me. Quote [sIGPIC]e[/sIGPIC]
AlexCode Posted May 24, 2005 Posted May 24, 2005 To add the event handler like you want: Dim asm As System.Reflection.Assembly 't = Type of the object where the event handler procedure is 'You can use the referenced assembly to get this type: t = asm.GetType("Calss...path") Dim t As Type Dim c As System.Windows.Forms.Control 'Control reflected Dim mymethod As System.Reflection.MethodInfo = t.GetMethod("Procedure Name") Dim pEv As System.Reflection.EventInfo = Me.GetType.GetEvent("Click") Dim pDel As [Delegate] = [Delegate].CreateDelegate(pEv.EventHandlerType, mymethod) pEv.AddEventHandler(c, pDel) Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
APaule Posted May 25, 2005 Posted May 25, 2005 Hey, thats cool, thanks a lot. :) :) :) But there are two minor questions left. 1.)Dim mymethod As System.Reflection.MethodInfo = t.GetMethod("BClick")This only detects public methods. Even when I extended the line tot.GetMethod("BClick", BindingFlags.NonPublic)it returned nothing. The method had to be public. 2.)Dim pDel As [Delegate] = [Delegate].CreateDelegate(pEv.EventHandlerType, mymethod)At that line an exception was thrown. The method has to be static. Ok, 2.) is not a question; I can live with that, though it might have implications with declaration of variables used in that method, but 1.) is strange. Am I doing something wrong, or is it a bug? Quote
APaule Posted May 25, 2005 Posted May 25, 2005 Just found out something: When you extend the line tot.GetMethod("BClick", BindingFlags.NonPublic Or BindingFlags.Static)it also works with private methods - but only if it's static as well. It's really strange that this methods doesn't return any method that is just private. It HAS to be declared private shared. In this case it doen't matter, because the CreateDelegate methods needs a static one, but in other cases.... Any idea? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.