예제 #1
0
        public Type GetType <T>(string objectType, string dll, ref string dyDll, bool tryT) where T : class
        {
            Type   t     = null;
            string tType = tryT && typeof(T) != typeof(object) && typeof(T).FullName != objectType && !typeof(T).IsInterface && !typeof(T).IsAbstract ? typeof(T).FullName : null;

            if (_dyDllConfig != null)     //There are specified dyDll, then to App_Data / Config / Dll under the search, and there must be type, do not try T (/ / Has specified dyDll, then to App_Data / Config / Dll under the search, and there must be type, do not try T)
            {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                //Assembly[] asses = null;
                string[] files = System.IO.Directory.GetFiles(_dyDllConfig.getPath(), "*.dll");

                for (int i = 0; i < files.Length; i++)
                {
                    /*Assembly dynamicAss = Assembly.LoadFrom(files[i]);
                     * t = dynamicAss.GetType(objectType,false);
                     * if (t != null)
                     *  break;*/
                    byte[]   assemblyBytes = File.ReadAllBytes(files[i]);
                    Assembly dynamicAss    = currentDomain.Load(assemblyBytes);
                    t = dynamicAss.GetType(objectType, false);
                    if (t != null)
                    {
                        break;
                    }
                }
            }
            else
            {
                Assembly[] asses = System.AppDomain.CurrentDomain.GetAssemblies();
                Dictionary <string, int> repeatAsses = new Dictionary <string, int>();
                foreach (Assembly ass in asses)
                {
                    if (!ass.GlobalAssemblyCache)
                    {
                        if (!repeatAsses.ContainsKey(ass.FullName))
                        {
                            repeatAsses.Add(ass.FullName, 0);
                        }
                        repeatAsses[ass.FullName]++;
                    }
                }
                foreach (Assembly ass in asses)
                {
                    //Not find. Net itself as a service object?
                    if (!ass.GlobalAssemblyCache)
                    {
                        t = ass.GetType(objectType, false);
                        if (t != null)
                        {
                            // TODO: Exclude those who are dynamic dll, but can not find, that obsolete, a new version
                            // TODO: If you need to reference in the app or dll third-party dll, do not want it into the bin directory, it is possible
                            // But with the version number, keep consistent

                            // This is usually used to find the way, if not cache, expired, there should be the latest version of the
                            if (repeatAsses[ass.FullName] == 1)
                            {
                                break;
                            }
                        }
                        else if (tType != null)
                        {
                            t = ass.GetType(tType, false);
                            if (t != null)
                            {
                                if (repeatAsses[ass.FullName] == 1)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (t == null && tType != null)
            {
                t = typeof(T);
            }
            return(t);
        }
예제 #2
0
        public Type GetType <T>(string objectType, string dll, ref string dyDll, bool tryT) where T : class
        {
            Type   t     = null;
            string tType = tryT && typeof(T) != typeof(object) && typeof(T).FullName != objectType && !typeof(T).IsInterface && !typeof(T).IsAbstract ? typeof(T).FullName : null;

            if (_dyDllConfig != null)     //有指明dyDll,則到App_Data/Config/Dll下搜,且一定有type,也不再試T
            {
                Assembly[] asses = null;
                string []  files = System.IO.Directory.GetFiles(_dyDllConfig.getPath(), "*.dll");
                for (int i = 0; i < files.Length; i++)
                {
                    Assembly dynamicAss = Assembly.LoadFrom(files[i]);
                    t = dynamicAss.GetType(objectType, false);
                    if (t != null)
                    {
                        break;
                    }
                }
            }
            else
            {
                Assembly[] asses = System.AppDomain.CurrentDomain.GetAssemblies();
                Dictionary <string, int> repeatAsses = new Dictionary <string, int>();
                foreach (Assembly ass in asses)
                {
                    if (!ass.GlobalAssemblyCache)
                    {
                        if (!repeatAsses.ContainsKey(ass.FullName))
                        {
                            repeatAsses.Add(ass.FullName, 0);
                        }
                        repeatAsses[ass.FullName]++;
                    }
                }
                foreach (Assembly ass in asses)
                {
                    //不找.net本身的類作服務對象?
                    if (!ass.GlobalAssemblyCache)
                    {
                        t = ass.GetType(objectType, false);
                        if (t != null)
                        {
                            //TODO:排除那些是動態dll,但是又找不到的,說明過時了,有新版本
                            //TODO:如果需要在app或dll中引用第三方dll,又不想把它放到bin目錄下,是可以的
                            //但是要用版本號,保持一致

                            //凡是用這種直接找的方式,如果不是cache,說明過期了,應該有最新版本在
                            if (repeatAsses[ass.FullName] == 1)
                            {
                                break;
                            }
                        }
                        else if (tType != null)
                        {
                            t = ass.GetType(tType, false);
                            if (t != null)
                            {
                                if (repeatAsses[ass.FullName] == 1)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            if (t == null && tType != null)
            {
                t = typeof(T);
            }
            return(t);
        }