Adam Rodger
Adam Rodger

Reputation: 3562

C# Generic Type Inference With Multiple Types

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);

Upvotes: 4

Views: 1749

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1504122

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);

Upvotes: 8

Vilx-
Vilx-

Reputation: 107062

Why not just write it like this:

public string SerialiseAs<TResult>(TResult input)
{
    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();
}

Since TInput derives from TResult, you really don't need to specify at all.

Upvotes: 7

Related Questions