C# - 在运行时加载程序集,在实例上调用的方法似乎无效
我正在尝试加载一个程序集,从该程序集中实例化一个类,然后调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设程序集正在工作,这是我用来完成相同任务的一段简化代码。
assuming the assembly is working, here is a simplified piece of code that I have used to accomplish the same task.