首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OpCodes.Call生成此代码

如何使用OpCodes.Call生成此代码
EN

Stack Overflow用户
提问于 2012-02-01 22:08:13
回答 2查看 1.9K关注 0票数 0

这个问题与:Casting items of a collection with code generation有关。

由于上一个问题不够明确,所以我需要确切的帮助。

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

代码语言:javascript
复制
return Enumerable.ToList<Potato>(Eumerable.Cast<Potato>(_proxyPotatoes));

下面是我试图做的事情的一个例子:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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>)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-02 05:20:27

这两个方法调用应该如下所示:

代码语言:javascript
复制
ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(typeof(Potato)));
ilg.Emit(OpCodes.Call, typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(typeof(Potato)));
票数 2
EN

Stack Overflow用户

发布于 2012-02-01 22:12:30

我有一个建议--用C#编写代码,编译它,并使用ILDASM来查看您需要发出什么。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9104085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档