From: Igor R. on
Hello,

I'd like to pass a type as a parameter to a function. For example:

void testTypeCast(Object o, Type t)
{
// of course, it doesn't compile
var casted = o as t;
}

and I'd like to call it like this:

testTypeCast(myObj, MyClass);

Is there a way to accomplish the above?

Thanks.
From: Alberto Poblacion on
"Igor R." <igor.rubinov(a)gmail.com> wrote in message
news:1d1a42d6-6256-4e70-82cb-fbeab13644de(a)d37g2000yqm.googlegroups.com...
> I'd like to pass a type as a parameter to a function. For example:
>
> void testTypeCast(Object o, Type t)
> {
> // of course, it doesn't compile
> var casted = o as t;
> }
>
> and I'd like to call it like this:
>
> testTypeCast(myObj, MyClass);
>
> Is there a way to accomplish the above?

No, not the way that you want it. The problem is not passing a Type
(which can be done precisely in the way you wrote). The "wrong" part is the
conversion itself. WHERE do you want to store the converted value? Well, you
want a variable of type t. But you can't declare a variable of that type
because it is not known at compile time (you wish to pass it as a
parameter). You also can`t use "var", because this one is also typed at
compiled time. So you need to declare a variable that can accept any type,
which limits you to "object" (or maybe "dynamic" if you are using C# 4.0).
So you need to do something like:

object casted = o as t;

The "o as t" part will not compile. But it is useless in general, since
you are going to store the result in an object anyway. So you might as well
do this:

object casted = o;

Of course, the "o as t" part does something else besides the cast: It
returns null if o is not t. System.Type has methods that let you compare the
type:

void testTypeCast(Object o, Type t)
{
object casted = null;
if (t.IsInstanceOfType(o))
casted = o;
}

From: Arne Vajhøj on
On 14-06-2010 12:11, Igor R. wrote:
> I'd like to pass a type as a parameter to a function. For example:
>
> void testTypeCast(Object o, Type t)
> {
> // of course, it doesn't compile
> var casted = o as t;
> }
>
> and I'd like to call it like this:
>
> testTypeCast(myObj, MyClass);
>
> Is there a way to accomplish the above?

I am skeptical about its usefulness but try:

T testTypeCast<T>(Object o) where T : class
{
return o as T;
}

Arne
From: Peter Duniho on
Igor R. wrote:
> Hello,
>
> I'd like to pass a type as a parameter to a function. For example:
>
> void testTypeCast(Object o, Type t)
> {
> // of course, it doesn't compile
> var casted = o as t;
> }
>
> and I'd like to call it like this:
>
> testTypeCast(myObj, MyClass);
>
> Is there a way to accomplish the above?

Assuming that your real method does something more interesting than just
actually casting the object, Arne's suggestion to use generics is a good
one.

Another alternative would be to use reflection. Specifically, from the
variable "t", you can call Type members such as GetMethod(),
GetProperty(), etc. which return new objects that can then be used to
access the object "o". But it's a much slower way to access members in
an object, and makes for more complicated, hard-to-understand code. So
you should only use that if it's absolutely necessary.

Pete