1

Today I encountered a small problem, and unfortunately I have not found a good solution. I would like to create a extension method for some generic interface. This method accepts a different type of a generic parameter, as seen below

public interface IGenericInterface<TSource>
{
    void DoSomething<TDest>();
}

DotNetFiddle

How to call a DoSomething2 method exactly in the same way as DoSomething method (without passing first type)?

public void DoSomething<TDest>()
//Called like below
testClass.DoSomething<object>();

instead of (like my current signature)

public static void DoSomething2<TDest, TSource>(this IGenericInterface<TSource> interf)
//called like below
testClass.DoSomething2<string, object>(); // have to repass object type

I know that the compiler can not automatically infer the second type (link). But I'm curious if anyone has found a solution.

4
  • 3
    In the future, please include all relevant code (as text) in your post. The fiddles are great, but they are no substitute for actual code. I've edited some of your code into your question. Commented Jun 8, 2015 at 12:44
  • 1
    That's not possible. In theory, c# could potentially infer the first type, but it either infers all type arguments or none, you can't have a partial infer. Commented Jun 8, 2015 at 12:45
  • @ryanyuyu thank you, i'll keep that in mind. Kenneth unfortunately, only such information come to me. It seems to me that there would be no problem if the compiler could infer the second type. In this case extension method it is hard to understand. Commented Jun 8, 2015 at 12:51
  • What's the problem you're actually trying to solve with this? It's quite possible there's a simpler, more idiomatic way to solve it. Commented Jun 8, 2015 at 13:08

1 Answer 1

2

Your problem is the argument - you're depending on type inference to handle the first generic type, but then you want to explicitly specify the second one - that just isn't possible in C#.

Instead, why not change the type of the argument?

public static void DoSomething3<T, T2>(this IGenericInterface<T> interf, T2 parameter)
//called like below
testClass.DoSomething3((object)3);

With this, T is inferred based on testClass (as long as it's not ambiguous), and T2 is inferred based on (object)3.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, I can not explicitly pass an object to this method. I fixed the content of the questions in order to better clarify this problem,
@PawelMaga Well, too bad. If you really want to avoid having to repeat the first argument, you could have a method like DoSomething<T, T2>(this ..., T2 argument), with the argument only being there for type inference - you could then call testClass.DoSomething3(default(object)) or testClass.DoSomething3(default(string)). You still need to know the proper type at runtime, of course.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.