Why must I provide explicitly generic parameter ty

2019-01-04 03:37发布

Why must I provide explicitly generic parameter types While the compiler should infer the type?

public static T2 Cast<T1,T2>(this T1 arg) where T2 : class where T1 : class
{
    return arg as T2;
}

Sample Usage:

 objOfTypeT2 = objOfTypeT1.Cast<TypeT1,TypeT2>();


Compared to my desired usage with a more intelligent compiler:

 objOfTypeT2 = objOfTypeT1.Cast<TypeT2>();

or maybe I should be more intelligent :-)

Beware that I provide the return type. I want to not provide the object that I called the function on it, the method is an Extension Method.

3条回答
We Are One
2楼-- · 2019-01-04 03:57

The specification limits type parameter inference for generic methods to all or nothing. You can't have partial inference.

The rationale is probably simplifying type inference rules (that are already pretty complex, as they have to take into account overloading rules too).

查看更多
Fickle 薄情
3楼-- · 2019-01-04 03:59

I've used Marc Gravell's solution and like it, but I can present another alternative.

Because the generic parameters are inferred from the parameters, another option is to use an out parameter for the result instead of a return value.

This have been possible for a long time, but today's C# allows you to declare the variable inline, which I find to be a usable flow.

public static void Cast<T1, T2>(this T1 arg, out T2 result) where T2 : class where T1 : class
{
    result = arg as T2;
}

You can call this as follows

objOfTypeT1.Cast(out Type2 objOfTypeT2);
查看更多
beautiful°
4楼-- · 2019-01-04 04:03

Inference doesn't consider the return type; you can, however, try splitting the generics; for example, you could write code to allow:

.Cast().To<Type2>()

by having (untested; indicative only)

public static CastHelper<T> Cast<T>(this T obj) {
    return new CastHelper<T>(obj);
}
public struct CastHelper<TFrom> {
    private readonly TFrom obj;
    public CastHelper(TFrom obj) { this.obj = obj;}
    public TTo To<TTo>() {
       // your code here
    }
}
查看更多
登录 后发表回答