Re: problems typecast,find by function/member name and getting "managed reference"



SunnyDrake <sunnydrake@xxxxx> wrote:
Jon ???????:
The normal way to overcome this is to use an interface if possible.

and then use Runtime.InteropServices._Type.GetInterface Method (String)
?

No, and then call methods in the interface.

it's Net 2.0 :( and i need to add interface for each class and then add
functions to get variables :(. And interface will be a weak side of
design.

I guess it depends on the exact situation - but I would *generally*
rather see a "strongly called" interface member than futz around with
reflection. It does depend though.

Otherwise, you'll need to use reflection to get the appropriate
MethodInfo and invoke it.
I *think* you're basically asking for reflection here, but I'm not
sure. Here's a sample program if you are:
static void CallMethod (Type type, object target,
string methodName)
{
MethodInfo method = type.GetMethod(methodName);
// The "null" here is because we're assuming
// the method has no parameters
method.Invoke (target, null);
}
thanks it's was a great help!.
now i only need find a way to get object by its name in string..

Objects don't have names. What exactly do you mean?

and still i'm not found a good solution to dynamicaly
typecast(unboxing) any *unknown type* to object at runtime.

Hang on - casting and unboxing are very different things. Casting at
runtime doesn't make sense because the point of casting is to give the
*compiler* more information, which clearly isn't relevant at runtime.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages


Loading