使用代码生成转换集合的项目 [英] Casting items of a collection with code generation

查看:85
本文介绍了使用代码生成转换集合的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#进行代码生成,我想在getter中强制转换一个后备字段。



这里是一个示例:

 公共类土豆
{
}

公共类ProxyPotato:土豆
{
}

公共类炖
{
私人ICollection< ProxyPotato> _proxy土豆;

//这是我要生成的代码(特别是转换部分)
public ICollection< Potato>土豆{得到{return _proxyPotatoes.Cast< Potato>()。ToList(); }}
}

我有这段代码可以生成属性,但是我没有知道如何执行Cast:

  private static void SetProperty(TypeBuilder builder,string propertyName,Type propertyType,FieldBuilder fieldBuilder)
{
const string GetterPrefix = get_;
const string SetterPrefix = set_;

//生成属性
var propertyBuilder = builder.DefineProperty(propertyName,PropertyAttributes.HasDefault,propertyType,null);


//属性获取器和设置器属性。
const MethodAttributes propertyMethodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual;

//定义getter方法。
var getterMethod = builder.DefineMethod(string.Concat(GetterPrefix,propertyName),propertyMethodAttributes,propertyType,Type.EmptyTypes);

//发出IL代码。
// ldarg.0
// ldfld,_field
// ret
ILGenerator getterILCode = getterMethod.GetILGenerator();
getterILCode.Emit(OpCodes.Ldarg_0);
getterILCode.Emit(OpCodes.Ldfld,fieldBuilder);
getterILCode.Emit(OpCodes.Ret);

//定义setter方法。
MethodBuilder setterMethod = builder.DefineMethod(
string.Concat(SetterPrefix,propertyName),
propertyMethodAttributes,null,new Type [] {propertyType});

//发出IL代码。
// ldarg.0
// ldarg.1
// stfld,_field
// ret
ILGenerator setterILCode = setterMethod.GetILGenerator();
setterILCode.Emit(OpCodes.Ldarg_0);
setterILCode.Emit(OpCodes.Ldarg_1);
setterILCode.Emit(OpCodes.Stfld,fieldBuilder);
setterILCode.Emit(OpCodes.Ret);

propertyBuilder.SetGetMethod(getterMethod);
propertyBuilder.SetSetMethod(setterMethod);
}


解决方案

强制转换扩展方法是方法调用。您需要发出以下命令:

您可以使用OpCodes.Call指令调用这些静态方法。


I'm doing code generation with C# and I would like to cast a backing field inside a getter.

Here is an example:

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(); } }
}

I have this code which can generate a property but I don't know how to execute a Cast:

private static void SetProperty(TypeBuilder builder, string propertyName, Type propertyType, FieldBuilder fieldBuilder)
{
  const string GetterPrefix = "get_";
  const string SetterPrefix = "set_";

  // Generate the property
  var propertyBuilder = builder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);


  // Property getter and setter attributes.
  const MethodAttributes propertyMethodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual;

  // Define the getter method.
  var getterMethod = builder.DefineMethod(string.Concat(GetterPrefix, propertyName), propertyMethodAttributes, propertyType, Type.EmptyTypes);

  // Emit the IL code.
  // ldarg.0
  // ldfld,_field
  // ret
  ILGenerator getterILCode = getterMethod.GetILGenerator();
  getterILCode.Emit(OpCodes.Ldarg_0);
  getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);
  getterILCode.Emit(OpCodes.Ret);

  // Define the setter method.
  MethodBuilder setterMethod = builder.DefineMethod(
  string.Concat(SetterPrefix, propertyName),
  propertyMethodAttributes, null, new Type[] { propertyType });

  // Emit the IL code.
  // ldarg.0
  // ldarg.1
  // stfld,_field
  // ret
  ILGenerator setterILCode = setterMethod.GetILGenerator();
  setterILCode.Emit(OpCodes.Ldarg_0);
  setterILCode.Emit(OpCodes.Ldarg_1);
  setterILCode.Emit(OpCodes.Stfld, fieldBuilder);
  setterILCode.Emit(OpCodes.Ret);

  propertyBuilder.SetGetMethod(getterMethod);
  propertyBuilder.SetSetMethod(setterMethod);
}

解决方案

The cast extension method is a method call. You need to emit the following:

return Enumerable.ToList<Potato>(Enumerable.Cast<Potato>(_proxyPotatoes));

You can call these static methods using the OpCodes.Call instruction.

这篇关于使用代码生成转换集合的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆