C# - 在运行时加载程序集,在实例上调用的方法似乎无效

发布于 2024-08-30 23:23:14 字数 1189 浏览 3 评论 0原文

我正在尝试加载一个程序集,从该程序集中实例化一个类,然后调用 Run(),这应该在此实例内设置一个属性。

加载程序集似乎工作正常,因为我能够列出类型,但被调用的方法似乎无效:位于新实例中的属性仍然设置为 null,尽管它应该设置为某个值。

我还尝试使用 type.InvokeMethod(...) 语法调用该方法。

加载程序集、调用构造函数并调用方法的方法:

private IEntryPoint ChargerAppli(AppInfo ai)
{
        string cheminAssemblies = "D:\\TFS\\OBL Microsoft\\Stages\\2010\\WPF\\Shell\\Shell\\Applications\\";

        Assembly a = Assembly.LoadFile(cheminAssemblies + ai.AssemblyName);
        Type type = a.GetType(ai.StartupClass);

        IEntryPoint instance = Activator.CreateInstance(type) as IEntryPoint;
        instance.Run();
        return instance;
}

IEntryPoint 接口:

public interface IEntryPoint
{
    FrameworkElement HostVisual { get; set; }
    void Run();
}

我尝试加载的 IEntryPoint 实现,它位于新程序集中:

class Bootstrap : IEntryPoint
{
    private FrameworkElement _visuel;

    public Bootstrap()
    {
        //do some work;
        this._visuel = new MainVisual();
    }

    public System.Windows.FrameworkElement HostVisual { get; set; }

    public void Run()
    {
        HostVisual = this._visuel;
    }
}

我可能缺少什么?

I'm trying to load an assembly, instantiate a class from that assembly, and then call Run(), which should set a property inside this instance.

Loading the assembly seems to work fine as I'm able to list the types, but the called method seems to be ineffective: the property located in the new instance remains set to null, depsite the fact that it should be set to something.

I also tried calling the method using a type.InvokeMethod(...) syntax.

Method that loads the assembly, calls the constructor and calls the method:

private IEntryPoint ChargerAppli(AppInfo ai)
{
        string cheminAssemblies = "D:\\TFS\\OBL Microsoft\\Stages\\2010\\WPF\\Shell\\Shell\\Applications\\";

        Assembly a = Assembly.LoadFile(cheminAssemblies + ai.AssemblyName);
        Type type = a.GetType(ai.StartupClass);

        IEntryPoint instance = Activator.CreateInstance(type) as IEntryPoint;
        instance.Run();
        return instance;
}

IEntryPoint interface:

public interface IEntryPoint
{
    FrameworkElement HostVisual { get; set; }
    void Run();
}

IEntryPoint implementation that I'm trying to load, which is located in the new assembly:

class Bootstrap : IEntryPoint
{
    private FrameworkElement _visuel;

    public Bootstrap()
    {
        //do some work;
        this._visuel = new MainVisual();
    }

    public System.Windows.FrameworkElement HostVisual { get; set; }

    public void Run()
    {
        HostVisual = this._visuel;
    }
}

What may I be missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吃不饱 2024-09-06 23:23:14

假设程序集正在工作,这是我用来完成相同任务的一段简化代码。

 Assembly assembly = Assembly.LoadFile(file);
 Type[] types = assembly.GetTypes();
 foreach (Type t in types)
 {
    MethodInfo[] methods = t.GetMethods();

   if (t.Name == "MyType")
   {
       foreach (MethodInfo method in methods)
       {
           if (method.Name == "Run")
           {
               try
               {
                   InterfaceToMyType activeModule = ("InterfaceToMyType")method.Invoke(null, args);
               }
               catch
               {
                    //do stuff here if needed
               }
           }
       }
   }
}

assuming the assembly is working, here is a simplified piece of code that I have used to accomplish the same task.

 Assembly assembly = Assembly.LoadFile(file);
 Type[] types = assembly.GetTypes();
 foreach (Type t in types)
 {
    MethodInfo[] methods = t.GetMethods();

   if (t.Name == "MyType")
   {
       foreach (MethodInfo method in methods)
       {
           if (method.Name == "Run")
           {
               try
               {
                   InterfaceToMyType activeModule = ("InterfaceToMyType")method.Invoke(null, args);
               }
               catch
               {
                    //do stuff here if needed
               }
           }
       }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文