【发布时间】:2014-06-05 20:09:59
【问题描述】:
我正在尝试使用 C# 中的 TypeBuilder 动态生成具有函数的类,并让该函数调用另一个基函数。
之所以需要这样做,是因为在 Revit 应用程序开发中,每个按钮都需要有一个类,该类使用 Execute 函数实现 IExternalCommand。我想动态创建按钮并在运行时根据它们的 ID 处理它们的执行,因此我也需要动态创建类。
希望这段代码能找到我正在寻找的东西(或这里http://pastebin.com/eehGKteT):
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Reflection.Emit;
namespace Centek_Revit_Addin
{
class DynamicButton
{
// I would like to use a function like this to generate the class during runtime, presumably using TypeBuilder:
public static void generateClass(int id)
{
// ... Code which would generate a class with the name "GeneratedClass" with the 'id' parameter appended at the end
// ... The class implements IExternalCommand
// ... The class has an Execute function with the parameters listed in the example, which returns a call to the Execute function in DynamicButton
// along with the added integer 'id' parameter at the end
}
public static Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements, int id)
{
TaskDialog.Show("About", "ID of the class that called us: " + id);
return Autodesk.Revit.UI.Result.Succeeded;
}
}
// ===== This class would have been generated during runtime using generateClass(15) ====== //
class GeneratedClass15 : Autodesk.Revit.UI.IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData revit, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
return DynamicButton.Execute(revit, ref message, elements, 15);
}
}
// =================================================================== //
}
我试图让 TypeBuilder 工作,并且我了解了基础知识,但我似乎无法弄清楚如何使用 Opcodes 来获得我想要的类。
所以基本上我正在寻找编写 generateClass(int id) 函数的帮助。任何帮助将不胜感激!
编辑:
I would like to add my progress:
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Reflection.Emit;
namespace Centek_Revit_Addin
{
class DynamicButton
{
// I would like to use a function like this to generate the class during runtime, presumably using TypeBuilder:
public static void generateClass(int id)
{
// ... Code which would generate a class with the name "GeneratedClass" with the 'id' parameter appended at the end
// ... The class implements IExternalCommand
// ... The class has an Execute function with the parameters listed in the example, which returns a call to the Execute function in DynamicButton
// along with the added integer 'id' parameter at the end
AssemblyName aName = new AssemblyName("DynamicAssemblyExample");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave);
// For a single-module assembly, the module name is usually
// the assembly name plus an extension.
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
// Create class which extends Object and implements IExternalCommand
Type[] implements = {typeof(IExternalCommand)};
TypeBuilder tb = mb.DefineType("GeneratedClass" + id, TypeAttributes.Public, typeof(Object), implements);
// Create 'Execute' function sig
Type[] paramList = {typeof(ExternalCommandData), typeof(string), typeof(ElementSet)};
MethodBuilder mbExecute = tb.DefineMethod("Execute", MethodAttributes.Public, typeof(Result), paramList);
// Create 'Execute' function body
ILGenerator ilGen = mbExecute.GetILGenerator();
ilGen.Emit(OpCodes.Nop);
ilGen.Emit(OpCodes.Ldarg_1);
ilGen.Emit(OpCodes.Ldarg_2);
ilGen.Emit(OpCodes.Ldarg_3);
ilGen.Emit(OpCodes.Ldc_I4_S, id);
Type[] paramListWID = { typeof(ExternalCommandData), typeof(string), typeof(ElementSet), typeof(int) };
ilGen.EmitCall(OpCodes.Call, typeof(DynamicButton).GetMethod("Execute"), paramListWID);
//ilGen.Emit(OpCodes.Ret);
tb.CreateType();
}
public static Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements, int id)
{
TaskDialog.Show("About", "ID of the class that called us: " + id);
return Autodesk.Revit.UI.Result.Succeeded;
}
}
// ===== This class would have been generated during runtime using generateClass(15) ====== //
class GeneratedClass15 : Autodesk.Revit.UI.IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData revit, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
return DynamicButton.Execute(revit, ref message, elements, 15);
}
}
// =================================================================== //
}
这段代码更接近,但运行时出现错误
System.TypeLoadException:“GeneratedClass99”类型中的方法“执行” 从程序集'DynamicAssemblyExample,版本= 0.0.0.0, Culture=neutral, PublicKeyToken=null' 没有实现。
当我在generateClass(..) 中调用CreateType(..) 时出现此错误
【问题讨论】:
标签: c# function class dynamic typebuilder