Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确定派生类是否以自身作为泛型参数实现泛型接口_C#_.net_Generics_Reflection - Fatal编程技术网

C# 确定派生类是否以自身作为泛型参数实现泛型接口

C# 确定派生类是否以自身作为泛型参数实现泛型接口,c#,.net,generics,reflection,C#,.net,Generics,Reflection,我已经看到了类似问题的答案,但没有找到一个符合以下所有标准的答案 当B从A继承时,如何确定类B是否满足以下条件: [B]不实现任何[附加]接口(通用或非通用) [A]实现一个通用接口,它自己的类型作为通用参数 以下 object o = new SomeObject(); bool result = (o.GetType().GetInterfaces()[0] == typeof(IKnownInterface<???>)); // ??? should be typeof(o

我已经看到了类似问题的答案,但没有找到一个符合以下所有标准的答案

当B从A继承时,如何确定类B是否满足以下条件:

  • [B]
    不实现任何
    [附加]
    接口(通用或非通用)
  • [A]
    实现一个通用接口,它自己的类型作为通用参数
以下

object o = new SomeObject();
bool result = (o.GetType().GetInterfaces()[0] == typeof(IKnownInterface<???>));
// ??? should be typeof(o). How to achieve this?
objecto=newsomeobject();
bool结果=(o.GetType().GetInterfaces()[0]==typeof(IKnownInterface));
// ??? 应为(o)类型。如何做到这一点?
我知道我可以从类型中获取接口名称字符串,类似于“NameSpace.ClassName+IKnownInterface'1[[SomeType,ModuleName,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]”“,但这看起来既不直观也不安全。另外,
'1
表示法是基于用于该接口的泛型类型的数量递增的

我不是走错了路,就是错过了一些愚蠢的事情。请告知。

尝试:

 o.GetType().GetInterfaces()[0] == 
                   typeof(IKnownInterface<>).MakeGenericType(o.GetType())
o.GetType().GetInterfaces()[0]==
typeof(IKnownInterface).MakeGenericType(o.GetType())

这应该可以做到

//get the two types in question
var typeB = b.getType()
var typeA = typeB.BaseType;
var interfaces = typeA.GetInterfaces();

//if the length are different B implements one or more interfaces that A does not
if(typeB.GetInterfaces().length != interfaces.length){
   return false;
} 

//If the list is non-empty at least one implemented interface satisfy the conditions
return (from inter in interfaces
        //should be generic
        where inter.IsGeneric
        let typedef = inter.GetGenericTypeDefinition()
        //The generic type of the interface should be a specific generic type
        where typedef == typeof(IKnownInterface<>) &&
        //Check whether or not the type A is one of the type arguments
              inter.GetGenericTypeArguments.Contains(typeA)).Any()
//获取有问题的两种类型
var typeB=b.getType()
var typeA=typeB.BaseType;
var interfaces=typeA.GetInterfaces();
//如果长度不同,B实现一个或多个A没有的接口
if(typeB.GetInterfaces().length!=interfaces.length){
返回false;
} 
//如果列表为非空,则至少有一个实现的接口满足这些条件
返回(从接口中的inter
//应该是通用的
其中inter.com是泛型的
让typedef=inter.GetGenericTypeDefinition()
//接口的泛型类型应该是特定的泛型类型
其中typedef==typeof(IKnownInterface)&&
//检查类型A是否为类型参数之一
inter.GetGenericTypeArguments.Contains(typeA)).Any()

如果
B
继承自
A
A
不可能实现接口而
B
不实现接口
B
,根据定义,实现了
A
所做的所有接口。我将更新我的问题。我的意思是B不实现A没有的任何附加接口。你能解释一下为什么你想这样检查你的开发者吗?单元测试应该指向黑盒,实现应该无关紧要。考虑一个<代码> IEmailer <代码>的例子,它是从工厂内部实例化的,它要求<代码> iStimaby<代码>(我知道但是……)。只要它能像电子邮件一样工作,你为什么还在乎它是什么?您正在尝试对实现进行单元测试,而不是对functionality@Basic:单元测试可能不是实现这一点的理想方法,但它确实很方便。在我的场景中,我有一些重要的类,它们的作用域非常狭窄,不应该做任何超出预期的事情。换句话说,开发人员的小自由可能会破坏系统。这是我们对此类课程进行的许多简单的质量控制前检查之一。它使代码逻辑验证和测试更加简单。谢谢。你介意举一个这样可以避免的问题的例子吗?我以前从未见过这种测试,我很想知道。请不要回答,我宁愿:)哈!首先,我甚至不知道可以使用空的
通用符号。其次,这难道不意味着
IKnownInterface
可以是任何泛型类型吗?我理解您对
MakeGenericType
的回答,但我只想知道如何解释
符号。另外,我如何判断
[o]
是否实现了它的基类没有实现的任何接口?是否存在某种BindingFlag,或者您是否必须为这两种类型枚举接口并进行比较?
typeof(…)
表示
IKnownInterface
的任何通用变体
MakeGeneric
专门返回
IKnownInterface
@RaheelKhan的正确类型,您无需比较这两个类的接口。您只需比较实现的接口数量。因为B将拥有A的所有接口,所以长度不同的唯一方法是如果B实现了一些A没有实现的接口(完整性请参见我的答案)@RuneFS:谢谢!我应该想到:)@Basic:谢谢。那会很有价值的,谢谢。我想知道为什么您的代码不需要像@NechytailoOleh的答案中那样包含
typeof(IKnownInterface)。MakeGenericType(o.GetType())
方法。@RaheelKhan有点需要。`let typedef=inter.GetGenericTypeDefinition()`下面一行是我看到的sameAh。我更喜欢NechytailoOleh的简洁比较,但这两点都得到了回答。再次感谢。@RaheelKhan精简版仅适用于一个泛型类型参数。我的假设是,它应该适用于多种类型的参数,其中一种应该是AI know and agree。我的偏好完全是基于查询语法对我来说的“恶心”(尽管建议使用方法语法)。与函数无关,只是形式:P