程序集的完全限定名与动态加载程序
Type T = typeof(KQV1.DataAccess.ConnectionBuilder);
string s = T.Assembly.FullName.ToString();
Assembly SampleAssembly = Assembly.Load(s);
MessageBox.Show(s);
private Form LoadFromAsmbl(string strAsmblPath, string strClassName)
{
// 验证你的程序集是不是存在,你的总不能去引用一个不存在的东西吧?
if(!File.Exists(strAsmblPath))
{
MessageBox.Show("Assembly Not Exists!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}

// 这里就加载程序集了,有很多种方式。去MSDN上看看吧
Assembly asmbl = null;
try
{
asmbl = Assembly.LoadFrom(strAsmblPath);
if (asmbl == null)
{
throw new Exception("Fail to load assembly " + strAsmblPath);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}

// 实例化你要的对象,如果成功的话就没问题了*_*
Form frmController = null;
try
{
frmController = (Form)asmbl.CreateInstance(strClassName);
if (frmController == null)
{
throw new Exception("Fail to create instance of " + strClassName);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}

// 到这里你就可以放心了*_*
return frmController;
}
浙公网安备 33010602011771号