.NET 4.0(或更早版本)反射API是否可以解决通用类型参数?有关当前尝试,请参见ArgumentException注释后的两行。
[TestMethod]
public void TestGenericParameterTokenResolution()
{
Type genericParameter = typeof(List<>).GetGenericArguments()[0];
Assert.IsTrue(genericParameter.IsGenericParameter);
int metadataToken = genericParameter.MetadataToken;
// make sure the metadata token is a GenericParam
Assert.AreEqual(metadataToken & 0xFF000000, 0x2A000000);
Module module = typeof(List<>).Module;
// the following both throw an ArgumentException.
Type resolvedParameter = module.ResolveType(metadataToken);
resolvedParameter = (Type)module.ResolveMember(metadataToken);
Assert.AreSame(genericParameter, resolvedParameter);
}
最佳答案