C# 如何使用操作码。调用生成此代码

C# 如何使用操作码。调用生成此代码,c#,reflection,reflection.emit,il,C#,Reflection,Reflection.emit,Il,这个问题涉及: 由于前面的问题不够清楚,下面是我需要帮助的确切内容 如何使用操作码。调用生成此代码: return Enumerable.ToList<Potato>(Eumerable.Cast<Potato>(_proxyPotatoes)); 返回Enumerable.ToList(Eumerable.Cast(_proxypots)); 下面是我尝试做的一个例子: public class Potato { } public class ProxyPotat

这个问题涉及:

由于前面的问题不够清楚,下面是我需要帮助的确切内容

如何使用操作码。调用生成此代码:

return Enumerable.ToList<Potato>(Eumerable.Cast<Potato>(_proxyPotatoes));
返回Enumerable.ToList(Eumerable.Cast(_proxypots));
下面是我尝试做的一个例子:

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(); } }
}
公共类
{
}
公共类代理马铃薯:马铃薯
{    
}
公共炖菜
{
私人ICollection_ProxyPotations;
//这是我想要生成的代码(特别是演员)
公共ICollection马铃薯{get{return}proxypotos.Cast().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>)
调用类[mscorlib]System.Collections.Generic.IEnumerable`1[System.Core]System.Linq.Enumerable::Cast(类[mscorlib]System.Collections.IEnumerable)
调用类[mscorlib]System.Collections.Generic.List`1[System.Core]System.Linq.Enumerable::ToList(类[mscorlib]System.Collections.Generic.IEnumerable`1)

我有一个建议-用C#编写代码,编译并使用ILDASM查看您需要发出的确切信息。

这两个方法调用应该类似于:

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

我认为这是一个很好的建议。这确实是一个很好的建议,但我仍然不知道如何生成我看到的IL代码(我添加了两行代码,我认为我需要生成为Edit 1)