[英]What might cause this ExecutionEngineException?
我正在嘗試使用Reflection.Emit在動態程序集中生成包裝器類。 自動包裝器生成是我正在編寫的名為“ GoInterfaces”的新開源庫的一部分。
包裝器類實現IEnumerable<string>
並包裝List<string>
。 用C#術語來說,它所做的就是:
class List1_7931B0B4_79328AA0 : IEnumerable<string>
{
private readonly List<string> _obj;
public List1_7931B0B4_79328AA0(List<string> obj)
{
this._obj = obj;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this._obj.GetEnumerator();
}
public sealed IEnumerator<string> GetEnumerator()
{
return this._obj.GetEnumerator();
}
}
但是,當我嘗試在包裝類上調用GetEnumerator()方法時,出現了ExecutionEngineException。 因此,我將動態程序集保存到DLL中,並在其上使用了ildasm。 以下代碼有什么問題嗎?
.class public auto ansi sealed List`1_7931B0B4_79328AA0
extends [mscorlib]System.Object
implements [mscorlib]System.Collections.Generic.IEnumerable`1<string>,
[Loyc.Runtime]Loyc.Runtime.IGoInterfaceWrapper
{
.field private initonly class
[mscorlib]System.Collections.Generic.List`1<string> _obj
.method public hidebysig virtual final instance
class [mscorlib]System.Collections.Generic.IEnumerator`1<string>
GetEnumerator() cil managed
{
// Code size 12 (0xc)
.maxstack 1
IL_0000: ldarg.0
IL_0001: ldfld class [mscorlib]System.Collections.Generic.List`1<string> List`1_7931B0B4_79328AA0::_obj
IL_0006: call instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<string>::GetEnumerator()
IL_000b: ret
} // end of method List`1_7931B0B4_79328AA0::GetEnumerator
.method public hidebysig virtual final instance
class [mscorlib]System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator() cil managed
{
.override [mscorlib]System.Collections.IEnumerable::GetEnumerator
// Code size 12 (0xc)
.maxstack 1
IL_0000: ldarg.0
IL_0001: ldfld class [mscorlib]System.Collections.Generic.List`1<string> List`1_7931B0B4_79328AA0::_obj
IL_0006: call instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<string>::GetEnumerator()
IL_000b: ret
} // end of method List`1_7931B0B4_79328AA0::System.Collections.IEnumerable.GetEnumerator
...
我有一個測試套件,其中包含各種不同的東西,包括從其他接口派生的接口,以及具有相同簽名的多個接口方法。 僅當我嘗試包裝IEnumerable<T>
,才會出現此問題。 如果有人願意,我很樂意發送源代碼(2個* .cs文件,沒有依賴項)。
List<T>
實際上有3個GetEnumerator()
方法; 它顯式實現IEnumerable.GetEnumerator()
和IEnumerable<T>.GetEnumerator()
,但是它還具有一個公共的GetEnumerator()
方法,該方法返回一個List<T>.Enumerator
實例,該實例是一個值類型。 您的代碼正在調用該方法,因此您需要在call
和ret
之間插入一個box
操作碼。
供以后參考,如果只編譯示例C#代碼並在Reflector中查看並將其與自己的IL進行比較,就很容易弄清楚。
IL的另一個問題是您的顯式接口實現不應公開,而應私有。 還有一些其他細微的差異,但是我不認為任何這些都會引起異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.