2012-03-23 39 views
0

我有一个项目,也有一个UserControlLibrary。我已经创建了UserControlLibary.Now的DLL,我想通过代码将它添加到我的应用程序中。将代码添加到WPF应用程序的DLL

我试过这个,但它不起作用。

Assembly a = Assembly.LoadFrom("user.dll"); 
     Type myType = a.GetType(); 
     UserControl user = (UserControl)a.CreateInstance(myType); 
     this.main_grid.Children.Add(user); 

我该怎么办?

+1

不工作是很少的信息,它抛出一个异常?如果是这样,请告诉我们这个例外。 – Silvermind 2012-03-23 10:21:42

+0

它在哪里失败?什么是例外? “不工作”真的很模糊! – 2012-03-23 10:21:51

+0

请更改dll的名称,以避免与系统混淆 – 2012-03-23 10:28:25

回答

0

也许你可以检查此:

Assembly assembly = Assembly.LoadFrom("DLLName"); 
Type[] listTypes = assembly.GetTypes(); 

for (int i = 0; i < listTypes.Length; i++) 
    Console.WriteLine("At position "+i+": "+listTypes[i].Name); 

然后,如果你的类型显示:

Type myType = listTypes[i]; 
FrameworkElement a = (FrameworkElement)Activator.CreateInstance(myType); 
this.main_grid.Children.Add(a); 
+0

非常感谢。 İt非常有帮助。我的问题解决了。 :) – 2012-04-02 15:21:11