簡體   English   中英

如何使用OpCodes.Call生成此代碼

[英]How to use OpCodes.Call to generate this code

此問題與以下內容有關: 使用代碼生成轉換集合的項目

由於前面的問題還不夠清楚,因此我需要准確的幫助。

如何使用OpCodes.Call生成此代碼:

return Enumerable.ToList<Potato>(Eumerable.Cast<Potato>(_proxyPotatoes));

這是我要執行的操作的一個示例:

public class Potato
{
}

public class ProxyPotato : Potato
{    
}

public class Stew
{
  private ICollection<ProxyPotato> _proxyPotatoes;

  //This is the code I would like to generate (specialy the cast part)
  public ICollection<Potato> Potatoes { get { return _proxyPotatoes.Cast<Potato>().ToList(); } }
}

編輯1

在@zmbq的建議之后,這是我需要生成的IL的兩行:

call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> [System.Core]System.Linq.Enumerable::Cast<class Maxime.Potato>(class [mscorlib]System.Collections.IEnumerable)

call class [mscorlib]System.Collections.Generic.List`1<!!0> [System.Core]System.Linq.Enumerable::ToList<class Maxime.Potato>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>)

我有一個建議-用C#編寫代碼,對其進行編譯,然后使用ILDASM來查看您需要發射的內容。

這兩個方法調用應類似於:

ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(typeof(Potato)));
ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(typeof(Potato)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM