使用两个泛型类型参数推断泛型类型

Tho*_*kow 5 c# generics type-inference

我有以下方法

public bool HasTypeAttribute<TAttribute, TType>(TType obj)
{
    return typeof(TType).GetCustomAttribute<TAttribute>() != null;
}
Run Code Online (Sandbox Code Playgroud)

我希望能够像这样使用它:

MyClass instance = new MyClass();

TypeHelper.HasTypeAttribute<SerializableAttribute>(instance);
Run Code Online (Sandbox Code Playgroud)

但我无法让它工作,因为

类型参数的数量不正确

所以我需要打电话

TypeHelper.HasTypeAttribute<SerializableAttribute, MyClass>(instance);
Run Code Online (Sandbox Code Playgroud)

这当然有道理,但为什么编译器不能推断传递对象的类型?因为如果方法看起来像这样:

public void Demo<T>(T obj)
{
}
Run Code Online (Sandbox Code Playgroud)

编译器肯定能够推断出类型,这样我就可以写

Foo.Demo(new Bar());
Run Code Online (Sandbox Code Playgroud)

代替

Foo.Demo<Bar>(new Bar());
Run Code Online (Sandbox Code Playgroud)

那么,有没有办法在这种情况下进行类型推断?这是我的设计缺陷还是我可以实现我想要的?重新排序参数也无济于事...

小智 6

可以将调用分成多个步骤,这样就可以在任何可能的地方进行类型推断。

public class TypeHelperFor<TType>
{
    public bool HasTypeAttribute<TAttribute>() where TAttribute : Attribute
    {
        return typeof(TType).GetCustomAttribute<TAttribute>() != null;
    }
}

public static class TypeHelper
{
    public static TypeHelperFor<T> For<T>(this T obj)
    {
        return new TypeHelperFor<T>();
    }
}

// The ideal, but unsupported
TypeHelper.HasTypeAttribute<SerializableAttribute>(instance);
// Chained
TypeHelper.For(instance).HasTypeAttribute<SerializableAttribute>();
// Straight-forward/non-chained
TypeHelper.HasTypeAttribute<SerializableAttribute, MyClass>(instance);
Run Code Online (Sandbox Code Playgroud)

对于这种情况,这应该可以正常工作,但我警告不要在最终方法返回 void 的情况下使用它,因为如果您不对返回值执行任何操作,则很容易使链处于半成形状态。

例如

// If I forget to complete the chain here...
if (TypeHelper.For(instance)) // Compiler error

// But if I forget the last call on a chain focused on side-effects, like this one:
// DbHelper.For(table).Execute<MyDbOperationType>();
DbHelper.For(table); // Blissfully compiles but does nothing

// Whereas the non-chained version would protect me
DbHelper.Execute<MyTableType, MyDbOperationType>(table);
Run Code Online (Sandbox Code Playgroud)


Jon*_*nna 5

因为 C# 规则不允许这样做。

C# 有一个规则是可行的,如果某些类型与参数相关(因此至少在某些时候可以推断)并且显式给定类型的数量与剩余不可推断的数量相同类型,那么两者将协同工作。

这需要有人提出建议,说服其他参与 C# 决策的人这是一个好主意,然后才能实施。这还没有发生。

除了特性开始时必须证明自己值得它们带来的额外复杂性这一事实(向语言添加任何东西,它会立即变得更加复杂,工作越多,编译器错误的可能性越大等),那么问题是,它是一个好主意?

另外,您在这个特定示例中的代码会更好。

不利的一面是,现在每个人的代码都稍微复杂了一些,因为错误的方式可能会更多,从而导致代码在运行时失败而不是编译时失败,或者导致错误消息不太有用。

人们已经发现一些关于推理的案例令人困惑,这表明添加另一个复杂案例无济于事。

这并不是说这绝对是一个坏主意,只是有优点和缺点使它成为一个意见问题,而不是明显的缺乏。