好吧,为什么不现在就添加代码呢
我有两个项目。项目1是主dll,项目2是仅包含在Civil 3D或Map 3D中工作的代码的dll。
项目1:不引用项目2。只创建一个新的命令或鼠标右键菜单项左右。在那里你可以检查你是否在Civil 3D中运行,如果不是,你可以显示一条很好的消息。
如果在Civil 3D中运行,可以加载单独的dll并调用所需的函数:
-
- #if DEBUG
- string dllFile = FileSystem.Functions.GetMyPath() + @"..\..\..\Project 2\bin\Debug\Project2.dll";
- #else
- string dllFile = FileSystem.Functions.GetMyPath() + "Project2.dll";
- #endif
- if (FileSystem.Functions.FileExist(dllFile) == false)
- {
- return ExportResult.ModuleNotFound;
- }
- // Load the dll
- Assembly assembly = Assembly.LoadFrom(dllFile);
- System.Type assemblyClass = assembly.GetType("Project2");
- if (assemblyClass == null)
- {
- return ExportResult.ModuleNotFound;
- }
- // var methodInfo = t.GetMethod("MyFunction", new Type[] { typeof(int), typeof(string) });
- MethodInfo methodInfo = assemblyClass.GetMethod("MyFunction"); // I don't use parameters here
- if (methodInfo == null)
- {
- return ExportResult.FunctionNotFound;
- }
- // object[] constructorParameters = new object[0];
- // constructorParameters[0] = 999; // First parameter.
- // constructorParameters[1] = 2; // Second parameter.
- // Create Instance of the Class (Constructor)
- object instance = System.Activator.CreateInstance(assemblyClass, null); // No parameters needed for the constructor so I use null
- // Function parameters
- object[] parameters = new object[2];
- parameters[0] = objectIds;
- parameters[1] = dataTable;
- // Run Method with optional parameters (I feed it with objectids and a datatable) en get result back (in this case I return a bool)
- bool success = System.Convert.ToBoolean(methodInfo.Invoke(instance, parameters));
- if (success == true)
- {
- return ExportResult.Correct;
- }
效果相当好。
|