如何使用OpCodes.Call生成此代码 [英] How to use OpCodes.Call to generate this code
本文介绍了如何使用OpCodes.Call生成此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此问题与以下内容有关:使用代码铸造集合项
This question is related to: Casting items of a collection with code generation
由于上一个问题还不够清楚,因此我需要准确地提供帮助。
Since the previous question was not clear enough, here is what I need help with precisely.
如何使用OpCodes.Call生成此代码:
How to use OpCodes.Call to generate this code:
return Enumerable.ToList<Potato>(Eumerable.Cast<Potato>(_proxyPotatoes));
以下是我要执行的操作的示例:
Here is an example of what I'm trying to do:
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的两行:
After the suggestion of @zmbq here is the two line of IL i need to generate:
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>)
推荐答案
两个方法调用应类似于:
The two method calls should look something like:
ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(typeof(Potato)));
ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(typeof(Potato)));
这篇关于如何使用OpCodes.Call生成此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文