CLI嵌套泛型类型和泛型方法

时间:2009-08-07 21:20:44

标签: .net reflection clr metadata

我偶然发现了实现定义的行为吗?

以下是上下文:

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());
}

1 个答案:

答案 0 :(得分: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);