I have a generic class which can be serialized:
MyOwnGenericClass<T>
So I want to deserialize it and if T is a String instance handle it, in another case I want to throw an exception.
How to know type of generic contains in MyOwnGenericClass<T> while deserializing?
To what class I have to cast following code?
new BinaryFormatter().Deserialize(fileStrieam);
That's really easy. Just use
objectlike so:and then do what you said you would do:
So you're not checking if
Tis astring. You're checking more than that: You're checking if obj is aMyOwnGenericClass< string >. Nobody said it will always be aMyOwnGenericClass< something >and our only headache is to find what that something is.You can send bools, strings, ints, primitive arrays of int, even a
StringBuilder. And then there's your entourage: you could sendMyOwnGenericClass< int >,MyOwnGenericClass< string >(and this is the only one you accept).