0

I need to call a method defined as:

public static T Deserialize<T>(Stream source)

In my code below I have determined the type:

string serializedObj = objbyte;

var typeSeperatorIndex = serializedObj.IndexOf(TypeSeperator);
var type = Type.GetType(serializedObj.Substring(0, typeSeperatorIndex));
var serialized = serializedObj.Substring(typeSeperatorIndex + 1);


byte[] byteAfter64 = Convert.FromBase64String(serialized);
MemoryStream afterStream = new MemoryStream(byteAfter64);

var result = Serializer.Deserialize<xxxxx>(afterStream);
return result;

I need to supply the xxxxx type argument to the Deserialize<> method. How do I get this from my type variable?

TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • Try searching, see [duplicate](http://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method). – CodeCaster Oct 20 '16 at 11:08
  • I did search but did not come across that post. Do you know if reflection is the only way to do this as it seems rather cumbersome and inefficient? – TheEdge Oct 20 '16 at 11:16
  • It's the first Google hit for "C# call generic method with Type", if that helps. And no, there's no other way: you have runtime information (a `Type`) and you need compile-time information (generic parameters). You can't do that without reflection. – CodeCaster Oct 20 '16 at 11:17
  • That will teach me to use the SO search box instead of Google ;-) – TheEdge Oct 20 '16 at 11:21

0 Answers0