newbie question: casting Type as object to function?

  • Thread starter Thread starter daisy
  • Start date Start date
D

daisy

Hi,

I'm trying to write a general-purpose function that will take a Type t
and an object o as parameters, and then do some work within the
function that includes casting the object as a Type t and accessing
o's members and methods. (The object will always be a struct, if that
helps or hinders the situation.)

Is there a nice way to do this, perhaps using delegate types? I've
tried many things and searched lots groups, with no luck. :(

Thanks!
Daisy
 
daisy said:
I'm trying to write a general-purpose function that will take a Type t
and an object o as parameters, and then do some work within the
function that includes casting the object as a Type t and accessing
o's members and methods. (The object will always be a struct, if that
helps or hinders the situation.)

Is there a nice way to do this, perhaps using delegate types? I've
tried many things and searched lots groups, with no luck. :(

Casting basically only helps to tell the compiler that you know more
about it at compile-time than the compiler would otherwise. In this
case, you don't - unless you know something about the type in advance,
you wouldn't actually gain anything from casting it.

As you don't (as far as I can tell) know anything about the type in
advance - so you'll have to do everything with reflection instead.
 
Daisy,

What you're asking can be pretty straightforward, but
becomes more complex as your "object" becomes more
obscure.

If you know that your object will only be of 2-3
different types, you can write a switch case to check the
object's type (no need to pass the type parameter, you
should be able to check object.GetType()).

object o;
switch(o.GetType()){
case Type.GetType("System.Int32") :
System.Int32 i = (System.Int32)o;
break;
}
}

Or, if your objects all follow a similar design pattern,
I'd recommend implementing an interface and using the
interface in your method to perform this similar
functionality like so:

class myClass : ISomeInterface {
...
}

public void main(...){
myClass bob = new myClass();
CallMyMethod(bob);
}

public void CallMyMethod(ISomeInterface var){
... //some code that uses var without knowing what
type it actually is
}


The third option, as Jon pointed out, is reflection,
which allows you to do some really powerful things with
late-binding. Like the first solution, you have to know
what method you specifically want to call. But unlike
the first solution, you can have that methodName as a
string or stored in a database and retrieve it when you
need it like so:

string myMethodName = "someMethod";
MyObjectType o = new MyObjectType();
o.GetType().InvokeMember(myMethodName,
System.Reflection.BindingFlags.InvokeMethod &
System.Reflection.BindingFlags.Public, null, null, null);

Good luck,

JER
 
It's preferable to do this by having the object implement an interface, and
then the method parameter can be that interface type. If you have to go to
reflection, things will be slower.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top