这个问题与:Casting items of a collection with code generation有关。
由于上一个问题不够明确,所以我需要确切的帮助。
如何使用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>)发布于 2012-02-02 05:20:27
这两个方法调用应该如下所示:
ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(typeof(Potato)));
ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(typeof(Potato)));发布于 2012-02-01 22:12:30
我有一个建议--用C#编写代码,编译它,并使用ILDASM来查看您需要发出什么。
https://stackoverflow.com/questions/9104085
复制相似问题