通过反射动态创建和添加UserControl的简单例子
2015-03-07 23:25:52 访问(1867) 赞(0) 踩(0)
try
{
Type type = null;
string dllName = @"C:\Projectes\SlowX\SRC\ClientCode\ClientApps\SlowX.FileCompareApp\bin\Debug\SlowX.FileCompareApp.exe";
string className = "SlowX.FileCompareApp.UserControls.MainList";
try
{
Assembly ass = Assembly.LoadFile(dllName);
type = ass.GetType(className, true, true);
}
catch (Exception err)
{
throw new Exception("创建控件(dllName = " + dllName + " | ClassName = " + className + ")时候发生异常:" + err.Message);
}
if (type == null)
return;
object[] ucParams = new object[] { };
UserControl uc
=
Activator.CreateInstance(type, ucParams) as UserControl;
uc.Dock = DockStyle.Fill;
tabPage1.Controls.Add(uc);
tabControl_Main.SelectedTab = tabPage1;
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
上一条:
下一条:
相关评论
发表评论