自由人

对你残酷的人就是你的恩人......
程序集的完全限定名与动态加载程序


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;
        }

posted on 2008-04-05 07:51  rudyshen  阅读(444)  评论(0编辑  收藏  举报