help me with typecasting problem ...

Posted by Anil Namde on Stack Overflow See other posts from Stack Overflow or by Anil Namde
Published on 2010-05-20T16:06:55Z Indexed on 2010/05/20 16:10 UTC
Read the original article Hit count: 189

Filed under:

I would like to create a function which can take two arguments object and type and then type cast the object to appropriate type using the type parameter. Is that possible ? how can i achieve it ?

public class TEST
{
    public int test;
}
object ot = new TEST();
Type type = typeof(TEST);
TEST t = (type)ot;

//Function will be something like this
public string SearializeObject(Object obj, Type t)
{
    //check if obj is of type t
    if(obj is of type t){
    //cast obj to type t to read it
    ((Type t)obj).someMethod
}
}

© Stack Overflow or respective owner

Related posts about c#