Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generic Type Inference With Multiple Types

Tags:

c#

.net

generics

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);
like image 681
Adam Rodger Avatar asked Dec 09 '22 00:12

Adam Rodger


1 Answers

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);
like image 117
Jon Skeet Avatar answered Dec 24 '22 08:12

Jon Skeet