根据非引用 dll 的属性执行方法

发布于 2024-10-19 01:24:13 字数 1724 浏览 2 评论 0原文

我有一个 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 并按以下顺序执行其函数:

  1. Begin()
  2. 每个具有属性 SomeAttribute
  3. 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:

  1. Begin()
  2. Each function with the attribute SomeAttribute
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2024-10-26 01:24:13

一旦有了 MethodInfo,您就可以对其使用 Invoke。如果从 GetMembers 返回的 MemberInfo 实际上指向一个方法,则可以将其强制转换为 MethodInfo。因此,一旦获得成员信息,您就可以使用此代码:

var method = mi as MethodInfo;
if (method != null) 
    method.Invoke(plugin, null);

您还可以创建代表该方法的委托。如果您需要多次调用它,这更合适。

Action action = (Action) Delegate.CreateDelegate(typeof(Action), plugin, method);
// Calls the method pointed to by the MethodInfo 
action();

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:

var method = mi as MethodInfo;
if (method != null) 
    method.Invoke(plugin, null);

You can also create a delegate that represents the method. This is more suitable if you need to call it more than once.

Action action = (Action) Delegate.CreateDelegate(typeof(Action), plugin, method);
// Calls the method pointed to by the MethodInfo 
action();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文