CIL中的字符串构建器(MSIL)

时间:2009-10-20 20:08:19

标签: c# token cil dynamicmethod

我正在尝试生成带有StringBuilder的代码,并将类中所有属性的值写入字符串。我有以下内容,但我目前在以下代码中获得“无效方法令牌”:

    public static DynamicAccessor<T> CreateWriter(T target) //Target class to *serialize*
    {
        DynamicAccessor<T> dynAccessor = new DynamicAccessor<T>();

        MethodInfo AppendMethod = typeof(StringBuilder).GetMethod("Append", new[] { typeof(Object) }); //Append method of Stringbuilder

        var method = new DynamicMethod("ClassWriter", typeof(StringBuilder), new[] { typeof(T) }, typeof(T), true);
        var generator = method.GetILGenerator();
        LocalBuilder sb = generator.DeclareLocal(typeof(StringBuilder)); //sb pointer


        generator.Emit(OpCodes.Newobj, typeof(StringBuilder)); //make our string builder 
        generator.Emit(OpCodes.Stloc, sb);                     //make a pointer to our new sb


        //iterate through all the instance of T's props and sb.Append their values.
        PropertyInfo[] props = typeof(T).GetProperties();
        foreach (var info in props)
        {
            generator.Emit(OpCodes.Callvirt, info.GetGetMethod()); //call the Getter
            generator.Emit(OpCodes.Ldloc, sb);                     //load the sb pointer
            generator.Emit(OpCodes.Callvirt, AppendMethod);        //Call Append 
        }

        generator.Emit(OpCodes.Ldloc, sb);
        generator.Emit(OpCodes.Ret);           //return pointer to sb

        dynAccessor.WriteHandler = method.CreateDelegate(typeof(Write)) as Write;
        return dynAccessor;
    }

有什么想法吗? 在此先感谢:)

1 个答案:

答案 0 :(得分:5)

任何值类型(int等)的属性都需要装箱;或者您需要使用不同的Append重载。

此外:

  • 您需要每次加载对象(arg0)
  • StringBuilder.Append是一个流畅的API;您需要弹出值,或重复使用它:
  • 因此,您不需要字段

(就个人而言,我会返回string,但“meh”)

像这样:

    DynamicAccessor<T> dynAccessor = new DynamicAccessor<T>();
    MethodInfo AppendMethod = typeof(StringBuilder).GetMethod("Append", new[] { typeof(Object) }); //Append method of Stringbuilder

    var method = new DynamicMethod("ClassWriter", typeof(StringBuilder), new[] { typeof(T) }, typeof(T), true);
    var generator = method.GetILGenerator();
    generator.Emit(OpCodes.Newobj, typeof(StringBuilder).GetConstructor(Type.EmptyTypes)); //make our string builder 
    //iterate through all the instance of T's props and sb.Append their values.
    PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (var info in props)
    {   
        generator.Emit(OpCodes.Ldarg_0);
        generator.Emit(OpCodes.Callvirt, info.GetGetMethod()); //call the Getter
        if (info.PropertyType.IsValueType)
        {
            generator.Emit(OpCodes.Box, info.PropertyType);
        }
        generator.Emit(OpCodes.Callvirt, AppendMethod);        //Call Append 
    }
    generator.Emit(OpCodes.Ret);           //return pointer to sb

这会产生相当于:

StringBuilder ClassWriter(T obj) {
    return new StringBuilder.Append((object)obj.Foo).Append((object)obj.Bar)
                   .Append((object)obj.Blip).Append((object)obj.Blap);
}