0

Possible Duplicate:
Why doesn’t C# infer my generic types?

Let's say we have such a method and a class:

Result<TOut> SendMessage<TIn, TOut>(TIn message)
    where TIn : IMessage<TOut>
{
}

class Message : IMessage<string> 
{
}

Why can't it resolve types when I invoke it like so:

var message = new Message();
var result = SendMessage(message);

We have Tin and we have constraint on Tin, so from there we can know Tout. But why it's not working?

If I write

var result = SendMessage<Message, string>(message);

It obviously works, but this is not much convenient.

Community
  • 1
  • 1
werat
  • 59
  • 1
  • 7

2 Answers2

0

Based on the specification, type inference occurs on input parameters and not return types.

You can get around your issue by including the output generic type parameter in your argument list:

 public static Result<TOut> SendMessage<TOut>(IMessage<TOut> message)
armen.shimoon
  • 6,303
  • 24
  • 32
  • That would fix this problem, but I need the Tin type for Service Locator to resolve IHandler where T is type of a message. – werat Nov 24 '12 at 09:17
-2

I think it is the syntax rule of Generic method. If you declare a method as Generic, you always need to provide the types when you invoke the method.

Colin
  • 551
  • 2
  • 10