c#动态加载dll中的类

    public class SchemaAssmbly
    {
        private string assmblyFile = "";
        private string objectType = "";
        public object realObject = null;

        public SchemaAssmbly(string assmblyFile, string objectType)
        {
            this.assmblyFile = assmblyFile;
            this.objectType = objectType;
        }

        public bool CreateRealObject()
        {
            if (realObject == null)
            {
                try
                {
                    if (System.IO.File.Exists(this.assmblyFile))
                    {
                        System.Reflection.Assembly assmbly = System.Reflection.Assembly.LoadFile(assmblyFile);
                        if (assmbly != null)
                        {
                            object theObject = assmbly.CreateInstance(objectType, true);
                            if (theObject != null)
                            {
                                realObject = theObject;
                                return true;
                            }
                        }
                    }
                    return false;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return true;
        }
    }

posted on 2008-06-16 10:36  步走高飞  阅读(680)  评论(0编辑  收藏  举报

导航