Is there way to typecast an object to some specific type at runtime? Is it possible at all?
public static void TryTypeCasting(object obj)
{
Type type = obj.GetType();
// Can I use this "type" variable somehow to typecast "obj" to its actual type?
}
I am using C# 4.0.
EDIT 1:
Thanks all for your inputs.
I may be trying to achieve something impossible. But I posted this question in order to get views of experts on this and to know if something like this is made achievable in C# 4.0.
Here is a real time problem:
In our product, our client side API (method) serializes instance of “some” class (say Employee) that derives from our entity class called Person. That serialized instance (i.e. string value) is sent to the server side API (a method which has responsibility to de-serialize string to appropriate class’s instance) through some intermediate classes. So, on the server side, only thing that API gets is a string.
However while serializing, custom serializer always add fully qualified name of the class (whose instance is being serialized) as the first line of the resulting output. So on server side, while reading the first line, I know the class (i.e. Employee in this case) to which the string should be de-serialized.
Further, we call a web service method (which I am not allowed to change) that accepts an argument of type Person.
Now, after deserialization at this stage, I have an instance of Employee stored in a variable of type object. But even though instance is available, I cannot pass it as an argument until I typecast it to Employee. How can I achieve this?
Sample code is provided here:
public static void Deserialize(string serializedObject)
{
StringReader stringReader = new StringReader(serializedObject);
// Read the first line to know class and assembly details
string firstLine = stringReader.ReadLine();
string[] assemblyAndClassDetails = firstLine.Split(new[] { ',' }, StringSplitOptions.None);
string className = assemblyAndClassDetails[0];
string assemblyName = assemblyAndClassDetails[1];
// Remove the first line before passing it to the serializer
serializedObject = serializedObject.Remove(0, firstLine.Length);
// Know the type of the serialized instance
Type typeToBeDeserializedTo = Type.GetType(className);
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeToBeDeserializedTo);
using(MemoryStream memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(serializedObject)))
{
memoryStream.Position = 0;
object deserializedObject = dataContractJsonSerializer.ReadObject(memoryStream);
// NOW I WANT TO call a method that accepts an argument of type `Person` How can I do this?
}
}
No, this is entirely impossible (unless of course the specific type is known at compile time, in which case you can e.g. hardcode a cast).
It could never be any other way, since type casting means that the compiler has full knowledge of what the type of the result is. How would it be possible for the compiler to have knowledge of something that can only be determined at runtime?
Whenever this question comes up (and it does every so often), the answer is “there is probably an appropriate solution for your situation that involve neither this hypothetical type of cast nor reflection”. If you state your case in more detail we could suggest such a solution.