2009-08-07 160 views
2

有我偶然发现了实现定义的行为?CLI嵌套泛型类型和泛型方法

这里是上下文:

public class GenericClass<T> 
{ 
    public class NestedGenericClass<U> 
    { 
     public void GenericMethod<K>() 
     { 
     } 
    } 
} 

这里的行为。这个单元测试按照书面通过。我的实际问题在“古怪”(如我现在看来)行为之前被列为评论。

[TestMethod] 
public void TestNestedGenericMethod() 
{ 
    Type openType = typeof(GenericClass<>.NestedGenericClass<>); 
    Type closedType = typeof(GenericClass<bool>.NestedGenericClass<int>); 
    /* Note there is absolutely no representation of the following as a [unique] type, via the 
    * typeof operator or the Reflection API, even though the metadata TypeSpec signature 
    * should in theory be able to reference it. This is the original reason I wrote these 
    * tests. 
    * Type partiallyOpenType = typeof(GenericClass<bool>.NestedGenericClass<>); 
    */ 

    MethodInfo openTypeOpenMethod = openType.GetMethod("GenericMethod"); 
    MethodInfo closedTypeOpenMethod = closedType.GetMethod("GenericMethod"); 
    MethodInfo closedTypeClosedMethod = closedTypeOpenMethod.MakeGenericMethod(typeof(long)); 

    Assert.IsNotNull(openTypeOpenMethod); 
    Assert.IsNotNull(closedTypeOpenMethod); 
    Assert.IsNotNull(closedTypeClosedMethod); 

    Assert.AreNotSame(openTypeOpenMethod, closedTypeOpenMethod); 
    Assert.AreNotSame(openTypeOpenMethod, closedTypeClosedMethod); 
    Assert.AreNotSame(closedTypeOpenMethod, closedTypeClosedMethod); 

    /* What on earth?! 
    * 1. Is the following covered in the CLI spec and/or is it implementation-defined? 
    * 2. Is there any potential use of this behavior (inside the runtime itself OR outside)? 
    * 3. Will I ever hit a MethodDefSig (§23.2.1)/MethodRefSig (§23.2.2)/MethodSpecSig (§23.2.15) that resolves to this? 
    */ 
    MethodInfo openTypeClosedMethod = openTypeOpenMethod.MakeGenericMethod(typeof(long)); 
    Assert.IsNotNull(openTypeClosedMethod); 
    Assert.AreNotSame(openTypeClosedMethod, openTypeOpenMethod); 
    Assert.AreNotSame(openTypeClosedMethod, closedTypeOpenMethod); 
    Assert.AreNotSame(openTypeClosedMethod, closedTypeClosedMethod); 

    Assert.AreSame(closedTypeOpenMethod, closedTypeClosedMethod.GetGenericMethodDefinition()); 
    Assert.AreSame(openTypeOpenMethod, openTypeClosedMethod.GetGenericMethodDefinition()); 
} 

回答

0

这不是太奇怪了:

//void GenericClass<>.NestedGenericClass<>.GenericMethod<Int64>() 
openTypeClosedMethod.ContainsGenericParameters = true 
openTypeClosedMethod.IsGenericMethodDefinition = false 

//void GenericClass<>.NestedGenericClass<>.GenericMethod<K>() 
openTypeOpenMethod.ContainsGenericParameters = true 
openTypeOpenMethod.IsGenericMethodDefinition = true 

//void GenericClass<bool>.NestedGenericClass<int>.GenericMethod<K>() 
closedTypeOpenMethod.ContainsGenericParameters = true 
closedTypeOpenMethod.IsGenericMethodDefinition = true 

//void GenericClass<bool>.NestedGenericClass<int>.GenericMethod<Int64>() 
closedTypeClosedMethod.ContainsGenericParameters = false 
closedTypeClosedMethod.IsGenericMethodDefinition = false 

MethodInfo closedGeneratedMethod = closedTypeClosedMethod.GetGenericMethodDefinition(); 
MethodInfo openGeneratedMethod = openTypeClosedMethod.GetGenericMethodDefinition(); 

//void GenericClass<bool>.NestedGenericClass<int>.GenericMethod<K>() 
closedGeneratedMethod.ContainsGenericParameters = true 
closedGeneratedMethod.IsGenericMethodDefinition = true 

//void GenericClass<>.NestedGenericClass<>.GenericMethod<K>() 
openGeneratedMethod.ContainsGenericParameters = true 
openGeneratedMethod.IsGenericMethodDefinition = true 

只是比较来自断言所有组合。

P.S.你错过了

Assert.AreNotSame(closedTypeOpenMethod, openTypeOpenMethod); 
+0

1)我没有错过这种情况(它的参数是相反的,但我有它)。 2)这些检查都不是在我的岗位的那些后显着,但我会加入他们还有更多,因为它们对API重要。 3)你没有回答我的任何3个问题,都是指一个事实,即ECMA-335的.NET Framework实现允许开放式泛型类型内封闭泛型方法。我的工作我自己的实现为乐趣的标准,我试图找出如果我要支持这种情况下,还是不行。 :) – 2009-08-07 23:12:06

+0

好了,比较遗憾的是“不起眼”后。 CLI无处不在显式禁止在一个开放的泛型类型中关闭泛型方法。它定义了泛型方法与其声明类型以及嵌套泛型类型之间的关系。仅CLI禁止部分构造类型和实例开放类型,所以我猜的方法不会太多的使用,至少在实例方法。至于MethodDefSig和其他,为什么不尝试将它发射到动态组装和从磁盘分析? – 2009-08-08 01:21:24