gpt4 book ai didi

.NET 错误 : ldtoken of an instantiated generic method

转载 作者:行者123 更新时间:2023-12-01 01:46:29 25 4
gpt4 key购买 nike

我正在尝试获取 ldtoken用于在泛型类中加载泛型方法的泛型实例化的 IL 方法(例如 List.ConvertAll<TOutput> ):

ldtoken method instance
class [mscorlib]System.Collections.Generic.List`1<!!0>
[mscorlib]System.Collections.Generic.List`1::ConvertAll<string>(
class [mscorlib]System.Converter`2<!0,!!0>)

这会导致异常:

Unhandled Exception: System.TypeLoadException: Could not load type 'System.Collections.Generic.List`1' from assembly 'TestAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.



它正在我的测试程序集中寻找 System.Collections.Generic.List , 即使它在 mscorlib 中明确声明!

但是,加载未实例化的方法 token 有效:
ldtoken method instance
class [mscorlib]System.Collections.Generic.List`1<!!0>
[mscorlib]System.Collections.Generic.List`1::ConvertAll<[1]>(
class [mscorlib]System.Converter`2<!0,!!0>)

非泛型类中的泛型方法有效:
ldtoken method void [mscorlib]System.Array::Sort<object>(!!0[])
ldtoken method void [mscorlib]System.Array::Sort<[1]>(!!0[])

这是怎么回事?这是 .NET 中的错误吗? (我已经在 v2 和 v4 CLR 上复制了它)

最佳答案

正如我在评论中提到的,我认为您需要实例化泛型类型(即没有像 System.Collections.Generic.List`1 这样的类型 - 只有 System.Collections.Generic.List`1<object> 等)。

通过使用 System.Reflection.Emit,看起来可以在实例化的泛型类型上使用带有实例化或未实例化方法的 ldtoken:

ldtoken 方法实例
类 [mscorlib]System.Collections.Generic.List`1
类 [mscorlib]System.Collections.Generic.List`1::ConvertAll<[1]>(
类 [mscorlib]System.Converter`2 )
ldtoken 方法实例
类 [mscorlib]System.Collections.Generic.List`1
类 [mscorlib]System.Collections.Generic.List`1::ConvertAll (
类 [mscorlib]System.Converter`2 )

但不适用于未实例化的泛型类型。 ECMA CLI 规范的第 II 部分第 9.4 节指出

The CLI does not support partial instantiation of generic types. And generic types shall not appear uninstantiated anywhere in metadata signature blobs.



所以也许这并不奇怪 - 似乎没有任何方法可以将 ldtoken 与 typeof(List<>).GetMethod("ConvertAll") 等效使用.

关于.NET 错误 : ldtoken of an instantiated generic method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10800240/

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