是否有可能限制一个C#泛型方法类型参数为"从&QUOT分配;包含类的类型参数? [英] Is it possible to constrain a C# generic method type parameter as "assignable from" the containing class' type parameter?

查看:115
本文介绍了是否有可能限制一个C#泛型方法类型参数为"从&QUOT分配;包含类的类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑,答案是否定的,但我想知道是否有可能做这样的事情:

I suspect the answer is no, but I want to know if it is possible to do something like this:

public class MyGenericClass<TSomeClass> {
    public void MyGenericMethod<TSomeInterface>() 
        // This doesn't compile.
        where TSomeClass : TSomeInterface 
    {
        //...
    }
}

我的意思与上述(非工作)来指示例如是约束 TSomeInterface 这样,它可以是任何的基类,实现的接口,或者(如果你真的想获得幻想)隐含 MyGenericClass 的转换。

What I mean to indicate in the above (non-working) example is to constrain TSomeInterface such that it can be any base class, implemented interface, or (if you really want to get fancy) implicit conversion of MyGenericClass.

注意:
我怀疑,为什么这是从来没有在C#中实现的原因是,通用的约束不是的真的的意思是代码的合同,这是多么我想在这里使用它们。我真的不在乎什么类型的 TSomeInterface 是,只要它是由 TSomeClass 实施

NOTE: I suspect that the reason why this was never implemented in C# is that generic constraints are not really meant to be code contracts, which is how I am trying to use them here. I really don't care what type TSomeInterface is, so long as it is implemented by TSomeClass.

到目前为止,我已经一起黑客攻击这样的:

So far, I have hacked this together:

public class MyGenericClass<TSomeClass> {
    public void MyGenericMethod<TIntermediateType, TSomeInterface>() 
        where TIntermediateType : TSomeClass, TSomeInterface 
    {
        //...
    }
}

这或多或少强制执行,我想约束(即 TSomeClass 必须继承或接口的情况下,实施 TSomeInterface ),但称它是非常笨拙,因为我必须指定 TIntermediateType (尽管我真的希望它评估对 TSomeClass ):

This more or less enforces the constraint that I want (that TSomeClass must inherit from, or in the case of an interface, implement, TSomeInterface), but calling it is very clumsy, because I have to specify TIntermediateType (even though I really want it to evaluate against TSomeClass):

var myGenericInstance = new MyGenericClass<TSomeClass>();
myGenericInstance.MyGenericMethod(TSomeClass, TSomeInterface);

此外,因为呼叫者在理论上可以指定<$ C $的一个子类以上的黑客被打破C> TSomeClass 作为第一个参数类型,其中只有子类实现 TSomeInterface

Additionally, the above hack is broken because a caller could in theory specify a subclass of TSomeClass as the first type parameter, where only the subclass implements TSomeInterface.

,我想这样做是我写一个WCF服务一口流利的工厂模式,我会的的,以防止来电(在编译时)从试图创建与该服务类没有实现合同的端点。我可以在运行时明显地检查这个(WCF其实做到这一点对我来说),但我的编译时检查的忠实粉丝。

The reason that I want to do this is that I am writing a fluent factory pattern for a WCF service, and I would like to prevent the caller (at compile time) from trying to create an endpoint with a contract that the service class doesn't implement. I can obviously check this at runtime (WCF in fact does this for me), but I am a big fan of compile-time checking.

有没有更好/更优雅的方式来实现我是什么这里经过?

Is there a better/more elegant way to achieve what I am after here?

推荐答案

我可以,为什么这并未环绕的原因,我的头的方式'T编译如下:

The way I was able to wrap my head around the reason why this doesn't compile is the following:

考虑这个程序编译:

class Program {
    class Class1 { }
    class Class2 { }
    public class MyGenericClass<TSomeClass> {
        public void MyGenericMethod<TSomeInterface>() where TSomeClass : TSomeInterface {
        }
    }
    static void Main(string[] args) {
        var inst = new MyGenericClass<Class1>();
    }
}



一切都很好。编译器是幸福的。现在考虑我修改方法:

static void Main(string[] args) {
    var inst = new MyGenericClass<Class1>();
    inst.MyGenericMethod<Class2>();
}



编译器会抱怨说 1级不执行类2 。但这行是错?约束是在调用 MyGenericMethod ,但问题的代码行 MyGenericClass 的创作。

The compiler will complain that Class1 does not implement Class2. But which line is wrong? The constraint is on the call to MyGenericMethod, but the offending line of code is the creation of MyGenericClass.

在换言之,一个得到红色的波浪线?

In other words, which one gets the red squiggly line?

这篇关于是否有可能限制一个C#泛型方法类型参数为&QUOT;从&QUOT分配;包含类的类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆