Loading methods in runtime

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am storing method names in one xml file , on run time i read method name
for ex class1.Method1 , I want invoke this method is there any facility in
system.reflection to load the methods like we load assemblies using
assembly.load. I would have instantiated the class before reading xml , Is
there any way I can load method on runtime.
Thanks in Advance
Sudheendra
 
Sudhee,

What does it mean "loading a methods"? You can load an assembly and a type
(a type is loaded when is first used). When you load a type all methods are
there at yours disposal - you can call them.
 
Hi,
Sorry I did not put it clearly. I have instatiated the Class 1 , it has let
us say method 1, Method2 . In my application I have xml file which has some
info and methodname like
event TagName="TextName"
type="System.Windows.Forms.KeyPressEventArgs"
valid="Class1.Ischar" .
I read this file run time and get method name ie valid --- class1.ischar to
a string . Now how to use this to invoke method presenlty I am doing like
this
Select case methodname
case "Class1.Ischar"
call Class1.Ischar

I want to avoid this case structure and directly call the method something
like
call <methodname> 'it contains Class1.Ischar
is this possible
thanks in advance
Sudhee
 
You can use the InvokeMember method of the Type class as follows:
Dim obj as Type = GetType(System.Windows.Forms.KeyPressEventArgs)
obj.InvokeMember("IsChar", ....)

You can examine the method to see other parameters needed.
 
Sudnee,

You need to get a type object first:
you can extract the class name from the string Class1.Ischar - Class1
then you need to get the type object from that neame - look at the
Type.GetType static methods.

Once you have the type object

Type t = Type.GetType(....);

you do

t.GetMethod("<method mane>",....).Invoke(object, ....)

You may need to use overloads of GetMethod and Invoke that accept
BindingFlag if the method is not public.

Also you need reference to an instance of the class in a case of non-static
methods.
 
Back
Top