我有一个用于编辑的窗口.编辑器应该加载一个dll(我完全控制它)以响应用户的选择,以了解如何直观地显示信息.(它们是dll,因为用户不一定想要或不需要每一个显示模型,并且还允许添加新的模型而不会弄乱主项目)
它们都将简单地存储在一个子目录中(现在无论如何)我很确定我可以枚举可用的dll但是我还需要做两件我不确定的事情.
1)从dll获取元数据的一些方法,所以我可以构建可能的显示选择列表......
2)加载选定的dll,并根据需要卸载它
任何建议将不胜感激.
如果您使用的是原始dll而不是.NET程序集,那么这里有一些方便的P/Invokes:
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern void SetDllDirectory(string lpPathName);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
privatestatic extern int GetModuleFileName(IntPtr module, [Out] StringBuilder fileName, int size);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static bool FreeLibrary(IntPtr module);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
Run Code Online (Sandbox Code Playgroud)
请注意,SetDllDirectory可能需要一些保护,因为它不适用于所有版本的Windows(Windows 2000,特别是没有它).
在使用中:
SetDllDirectory(candidateFolder);
IntPtr dllHandle = LoadLibrary(dllName);
if (dllHandle != IntPtr.Zero)
{
_dllHandle = dllHandle;
_location = candidateFolder;
_fullPath = Path.Combine(candidateFolder, dllName);
IntPtr p = GetProcAddress(_dllHandle, procName);
if (p == IntPtr.Zero)
throw new ArgumentException("procName");
SomeDelegateType d = (SomeDelegateType)Marshal.GetDelegateForFunctionPointer(p, typeof(SomeDelegateType));
d(/* args */);
}
Run Code Online (Sandbox Code Playgroud)
否则,您将使用汇编方法.查看程序集级别属性或对象级别属性是获取额外信息的好方法,但如果您想要的是插件系统,则应使用插件系统,如CodePlex上的托管外接程序框架.另见这个问题和答案.
| 归档时间: |
|
| 查看次数: |
8512 次 |
| 最近记录: |