gpt4 book ai didi

c# - 泛型类的 Module.ResolveMember 行为

转载 作者:行者123 更新时间:2023-11-30 16:39:46 25 4
gpt4 key购买 nike

我不了解具有通用参数的成员的 Module.ResolveMember 行为。根据下面的文档代码应该可以工作,但是 fooMember.DeclaringType 出于某种原因没有用 bool 类型参数化。

class A<T> where T:new()
{
T a;
public void foo()
{
a = new T();
}
}
class B:A<bool>
{ }
class Program
{
static void Main(string[] args)
{
var test = new B();
var aType = test.GetType().BaseType;
var token = aType.GetMember("foo").First().MetadataToken;
var fooMember = aType.Module.ResolveMember(token, aType.GetGenericArguments(), null);
Debug.Assert(aType == fooMember.DeclaringType);
Console.ReadKey();
}
}

为什么这行不通?

最佳答案

原来 ResolveMethod(Int32, Type[], Type[]) 需要 methodref 标记,对于泛型方法,我们有不同的 methodrefs 用于不同的泛型参数。 ResolveMethod 应该用于获取基于编译相关信息的方法,而不是解析泛型方法信息。

关于c# - 泛型类的 Module.ResolveMember 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52477986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com