C# WPF调用从另一个dll获取参数的方法
如何运行从另一个dll获取参数的方法? 我从另一个dll导入一个UserControl,如下所示,但我现在要么需要调用该UserControl中的方法,要么能够设置该类中包含的变量 加载用户控件C# WPF调用从另一个dll获取参数的方法,c#,wpf,C#,Wpf,如何运行从另一个dll获取参数的方法? 我从另一个dll导入一个UserControl,如下所示,但我现在要么需要调用该UserControl中的方法,要么能够设置该类中包含的变量 加载用户控件 UserControl ucSupportButton = new Bootstrapper().LoadUserControl("SC.Support.dll", "Button"); 引导程序中使用的代码 public UserControl LoadUserControl(st
UserControl ucSupportButton =
new Bootstrapper().LoadUserControl("SC.Support.dll", "Button");
引导程序中使用的代码
public UserControl LoadUserControl(string dllName, string loadType)
{
if (File.Exists(Path.Combine(applicationRoot, dllName)))
{
Assembly asm = Assembly.LoadFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), dllName));
Type[] types = asm.GetTypes();
Type type = types.Where(t => t.Name.Equals(loadType)).FirstOrDefault();
if (type != null)
{
return Activator.CreateInstance(type) as UserControl;
}
}
return null;
}
@高调评论似乎是最好的方式。根据您的设计,反射是另一种选择。您可以使用反射来获取该类型的方法或字段,然后调用或设置它
var method = paymentObjectInstance.GetType().GetMethod("MethodNameHere",
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
if (method == null)
{
return null;
}
var result = method.Invoke(paymentObjectInstance, null);
这里有点感谢MSDN。首先,如果这是WPF,那么您不应该在过程代码中操作UI元素,而这正是XAML的用途。第二,你基本上是在改造轮子,用MEF代替。微软已经做到了这一点,他们比你们和我更清楚这一点。