使用受约束泛型类型 C# 的静态方法
我有一个泛型类:
public class Foo<T> where T: Interface
{
}
T 被迫实现的接口在其内部定义了 2 个静态方法。
在构造函数中,我希望能够基本上执行以下操作:
public Foo()
{
value1 = T.staticmethod1();
value2 = T.staticmethod2();
}
这无法使用我上面发布的伪代码来完成。难道这样就不能调用这些静态方法了吗?
I have a generic class:
public class Foo<T> where T: Interface
{
}
the interface that T is being forced to implement has 2 static methods defined inside of it.
in the constructor I want to be able to basically do the following:
public Foo()
{
value1 = T.staticmethod1();
value2 = T.staticmethod2();
}
This cannot be accomplished with the psuedocode I have posted above. Is it not possible to call these static methods in this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您也许可以使用扩展方法。该技术被命名为pseudo-mixins。
尽管扩展方法实际上是静态的,但它们“假装”为实例方法,因此您仍然需要 T 的具体实例。
此外,如果您希望接口保留其作为自文档“契约”的角色,那么这就是一种作弊" 指定你的 T 类应该有哪些方法。
然而它们是类型安全的(如果你没有将 IBarExtensions 引入范围内,你的 Foo 类将无法编译
)
You may be able to use extension methods. This technique has been named pseudo-mixins.
Although the extension methods are actually static they "pretend" to be instance methods, so you still need a concrete instance of T.
Also, this is kind of cheating, if you want your interface to preserve its role as a self-documenting "contract" that specifies what methods your T class should have.
However they are type-safe (your Foo class will not compile, if you don't bring the IBarExtensions in scope)
testing
不,这是不可能的。即使使用
动态
也不行。有一些通用约束(例如接口),但仅适用于实例成员。您可以考虑将这些方法作为Func
/Action
委托传递到(参数)中?除此之外,你唯一的(也是不受欢迎的)选择就是反思。或者也许更好:重新思考我们的方法。
No, it is not possible. Not even with
dynamic
. There are generic constraints (such as interfaces), but that applies to instance members only. You could consider passing those methods in (parameters) asFunc<T>
/Action<T>
delegates?Beyond that, your only (and undesirable) option is reflection. Or perhaps better: rethink our approach here.
不可能使用受约束的泛型类型参数的静态成员,因为特定类型中静态成员的存在并不能说明派生类型是否具有与该名称兼容的成员。假设类型“Foo”有一个返回 Int32 的静态函数 Wowzo(),类型 DerivedFoo1 有一个不同的静态函数 Wowzo(),它也返回 Int32,类型 DerivedFoo2(派生自 Foo)有一个静态成员 Wowzo()返回一个 String,并且类型 DerivedFoo3 有一个名为 Wowzo 的嵌套类。如果 T 是一个类型参数,被限制为 Foo 的后代,那么 T.Wowzo 是什么?
It is not possible to use static members of constrained generic type parameters, because the existence of a static member in a particular type says nothing about whether a derived type will have a compatible member with that name. Suppose that type "Foo" has a static function Wowzo() that returns an Int32, type DerivedFoo1 has a different static function Wowzo() that also returns an Int32, type DerivedFoo2 (which derives from Foo), has a static member Wowzo() that returns a String, and type DerivedFoo3 has a nested class called Wowzo. If T is a type parameter constrained to be a descendant of Foo, what is T.Wowzo?
C# 不允许调用接口中定义的静态方法。它发出命名不当错误消息:
如果 C# 允许,您将使用接口名称而不是通用参数名称来调用它们:
因此,您有几个选择:
C# doesn't allow you to call static methods defined in interfaces. It issues a poorly named error message:
If C# would allow it, you'd use the interface name, and not the generic parameter name, to call them:
So, you have a couple of options:
您还可以使用非静态方法作为调用静态方法的包装器。然后,非静态方法可以成为接口的一部分。
You could also have a non static method as a wrapper calling your static method. The non static method can then be part of your interface.