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);
My question is, why can't TInput be inferred in this situation?
It can - it's TResult
which can't be inferred, and there's no way of specifying "partial" inference.
What you can sometimes do is separate the type parameters into ones for a generic type and ones for a generic method, so you'd end up with:
// Explicitly state TResult, and infer TInput
Serializer<MySuperType>.Serialize(x);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With