mardi 27 janvier 2015

C# Generics and Reflection - Passing an object to a generic method

I have a method with the following signature:



private string SerialiazeObj<T>(T obj)
{
// Do some work
}


Now, I have another method which accepts an object and calls the SerializeObj method, as shown below:



private void callSerializeObj(object obj)
{
Type objType = obj.GetType();
string s = SerialiazeObj<objType>((objType)obj));
}


The object passed to callSerializeObj can be of any type. Unfortunately, the compiler is giving me this error in the (string s = SerializeObj...) part:



The type or namespace 'objType' could not be found (are you missing an assembly reference).


I don't know if I am calling SerializeObj the correct way. What is the correct way of calling the method with an object which can be of any type?






Aucun commentaire:

Enregistrer un commentaire