根据非引用 dll 的属性执行方法
我有一个 Visual Studio 2008 C# .NET 3.5 项目,我接受非引用 DLL 作为插件。该插件实现了从已知接口派生的任意数量的类。每个类都从该接口实现一组已知函数,但也可能实现具有已知属性的未知函数。
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class SomeAttribute : Attribute
{
public SomeAttribute(string description) { /*...*/ }
}
public class PluginClassA : IPluginInterface
{
public PluginClassA(int some_val) : base(some_val)
{
}
public override void Begin() { /*do interesting things...*/ }
public override void End() { /*do interesting things...*/ }
[SomeAttribute("Attribute Title")]
public void SomeUnknownFunction() { /*do interesting things...*/ }
[SomeAttribute("Attribute Title")]
public void SomeOtherFunction() { /*do interesting things...*/ }
}
我希望能够加载该插件 DLL 并按以下顺序执行其函数:
- Begin()
- 每个具有属性
SomeAttribute
- End()
我有这样的东西:
static void Main(string[] args)
{
Assembly u = Assembly.LoadFile("Plugin.dll");
foreach (Type t in u.GetTypes())
{
if (t.GetInterface("IPluginInterface") != null)
{
IPluginInterface plugin = (IPluginInterface)Activator.CreateInstance(t, new int());
plugin.Begin();
foreach (MemberInfo mi in t.GetMembers())
{
if (mi.IsDefined(typeof(SomeAttribute), true))
{
// we found a member with the `SomeAttribute` attribute.
// how can I execute that method?
// I'd need something like a C++ function pointer to the function.
}
}
plugin.End();
}
}
}
谢谢, 保罗·H
I have a Visual Studio 2008 C# .NET 3.5 project where I accept a non-reference DLL as a plugin. The plugin implements an arbitrary number of classes derived from a known interface. Each class implements a set of known functions from that interface, but may also implement unknown functions that have a known attribute.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class SomeAttribute : Attribute
{
public SomeAttribute(string description) { /*...*/ }
}
public class PluginClassA : IPluginInterface
{
public PluginClassA(int some_val) : base(some_val)
{
}
public override void Begin() { /*do interesting things...*/ }
public override void End() { /*do interesting things...*/ }
[SomeAttribute("Attribute Title")]
public void SomeUnknownFunction() { /*do interesting things...*/ }
[SomeAttribute("Attribute Title")]
public void SomeOtherFunction() { /*do interesting things...*/ }
}
I'd like to be able to Load that plugin DLL and execute its functions in this order:
- Begin()
- Each function with the attribute
SomeAttribute
- End()
I have something like this:
static void Main(string[] args)
{
Assembly u = Assembly.LoadFile("Plugin.dll");
foreach (Type t in u.GetTypes())
{
if (t.GetInterface("IPluginInterface") != null)
{
IPluginInterface plugin = (IPluginInterface)Activator.CreateInstance(t, new int());
plugin.Begin();
foreach (MemberInfo mi in t.GetMembers())
{
if (mi.IsDefined(typeof(SomeAttribute), true))
{
// we found a member with the `SomeAttribute` attribute.
// how can I execute that method?
// I'd need something like a C++ function pointer to the function.
}
}
plugin.End();
}
}
}
Thanks,
PaulH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦有了 MethodInfo,您就可以对其使用 Invoke。如果从 GetMembers 返回的 MemberInfo 实际上指向一个方法,则可以将其强制转换为 MethodInfo。因此,一旦获得成员信息,您就可以使用此代码:
您还可以创建代表该方法的委托。如果您需要多次调用它,这更合适。
Once you have a MethodInfo, you can use Invoke on that. If the MemberInfo returned from GetMembers actually points to a method, you can cast it to MethodInfo. So you can use this code once you have the member info:
You can also create a delegate that represents the method. This is more suitable if you need to call it more than once.