如何使用 OpCodes.Call 生成此代码
此问题与以下内容相关:使用代码生成来转换集合的项目
由于上一个问题不够清楚,这就是我需要帮助的地方。
如何使用 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>)
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.
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(); } }
}
Edit 1
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>)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个建议 - 用 C# 编写代码,编译它并使用 ILDASM 来准确查看您需要发出的内容。
I have a suggestion - write the code in C#, compile it and use ILDASM to see exactly what you need to Emit.
这两个方法调用应该类似于:
The two method calls should look something like: