C# Reflection

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

Guest

Please, anyone can help on this:
I need to call a method from my class
but I need the information type at run time..as I know, I have to use
GetType method of the object class.
Any idea how to use this to call the method of my class ?

Regards,
Luiz
 
Luiz Ragazzi said:
Please, anyone can help on this:
I need to call a method from my class
but I need the information type at run time..as I know, I have to use
GetType method of the object class.
Any idea how to use this to call the method of my class ?

It's not at all clear what you're asking. What information do you
already have? Could you provide a short but complete program which
shows what you're trying to do, just with a line saying "I need to do
<x> here" where you're stuck?
 
Please, anyone can help on this:
I need to call a method from my class
but I need the information type at run time..as I know, I have to use
GetType method of the object class.
Any idea how to use this to call the method of my class ?

Regards,
Luiz

If you want help specifically with C# you might get a better response
on news://microsoft.public.dotnet.languages.csharp

rossum


The ultimate truth is that there is no ultimate truth
 
Hi Jon,
please take a look:
DataModel.DataLoader is my assembly, DmDataLoader.EUROBONUS is my class
I want to call the method DeleteAll from my class EUROBONUS.

I created an object myobj and call CreateInstance:
object myobj = new object();
myobj =
Activator.CreateInstance("DataModel.DataLoader","DmDataLoader.EUROBONUS");

line 1 and line 2 I've tryed the code:
line1:
myobj.GetType().InvokeMember("DeleteAll",BindingFlags.Public |
BindingFlags.InvokeMethod,null ,myobj,new object[] {"euro" });
the line 1: I got the message - "Method
System.Runtime.Remoting.ObjectHandle.DeleteAll not found."

line 2:
myobj.GetType().GetMethod("DeleteAll",BindingFlags.Public |
BindingFlags.CreateInstance | BindingFlags.Instance).Invoke( null,new
object[] { "euro"});
the line 2 I got "Object reference not set to an instance of an object."

PS.: "euro" is a string parameter passed to the method DeleteAll

thanks.
 
Luiz Ragazzi said:
please take a look:
DataModel.DataLoader is my assembly, DmDataLoader.EUROBONUS is my class
I want to call the method DeleteAll from my class EUROBONUS.

I created an object myobj and call CreateInstance:
object myobj = new object();

Why bother creating an object which you're then just discarding?
myobj =
Activator.CreateInstance("DataModel.DataLoader","DmDataLoader.EUROBONUS");

And does that part work?
line 1 and line 2 I've tryed the code:
line1:
myobj.GetType().InvokeMember("DeleteAll",BindingFlags.Public |
BindingFlags.InvokeMethod,null ,myobj,new object[] {"euro" });
the line 1: I got the message - "Method
System.Runtime.Remoting.ObjectHandle.DeleteAll not found."

Here you don't include BindingFlags.Instance - put that in and you may
well find it works.
line 2:
myobj.GetType().GetMethod("DeleteAll",BindingFlags.Public |
BindingFlags.CreateInstance | BindingFlags.Instance).Invoke( null,new
object[] { "euro"});
the line 2 I got "Object reference not set to an instance of an object."

Well, you don't want that, as that's asking for constructors rather
than methods.
PS.: "euro" is a string parameter passed to the method DeleteAll

If the above doesn't help, it would make things easier if you could
provide a short but complete program which demonstrates the
problem.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Back
Top