I have the following generic method for serialising an input object of one type as a super-type as follows:
public string SerialiseAs<TResult, TInput>(TInput input) where TInput : TResult
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(TResult));
MemoryStream stream = new MemoryStream();
ser.WriteObject(stream, input);
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
I have to call this method specifying both generic types as follows:
MySubType x = new MySubType();
string json = SerialiseAs<MySuperType, MySubType>(x);
My question is, why can't TInput be inferred in this situation? Is it because TResult isn't actually used as the return type? The following code is cleaner but won't compile because of the missing input type:
MySubType x = new MySubType();
string json = SerialiseAs<MySuperType>(x);