Exemple #1
0
        /// <summary>
        /// 查找所有非系统程序集中的所有插件
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ListX <Type> FindAllPlugins(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            ListX <AssemblyX> asms = GetAssemblies();
            ListX <Type>      list = new ListX <Type>();

            foreach (AssemblyX item in asms)
            {
                if (item.IsSystemAssembly)
                {
                    continue;
                }

                ListX <Type> ts = item.FindPlugins(type);
                if (ts != null && ts.Count > 0)
                {
                    list.AddRange(ts);
                }
            }
            return(list.Count > 0 ? list : null);
        }
Exemple #2
0
        /// <summary>
        /// 集合相加
        /// </summary>
        /// <param name="list1">第一个实体集合</param>
        /// <param name="list2">第二个实体集合</param>
        /// <returns></returns>
        public static ListX <T> operator +(ListX <T> list1, ListX <T> list2)
        {
            if (list1 == null || list1.Count < 1)
            {
                return(list2);
            }
            else
            {
                if (list2 == null || list2.Count < 1)
                {
                    return(list1);
                }
                else
                {
                    ListX <T> list = new ListX <T>();
                    list.AddRange(list1);
                    list.AddRange(list2);

                    return(list);
                }
            }

            //if ((list1 == null || list1.Count < 1) && (list2 == null || list2.Count < 1)) return null;

            //ListX<T> list = new ListX<T>();
            //if (list1 != null && list1.Count > 0) list.AddRange(list1);
            //if (list2 != null && list2.Count > 0) list.AddRange(list2);

            //return list;
        }
Exemple #3
0
        /// <summary>
        /// 根据指定项查找字符串。忽略大小写
        /// </summary>
        /// <param name="name">属性名</param>
        /// <param name="value">属性值</param>
        /// <returns></returns>
        public ListX <T> FindAllIgnoreCase(String name, String value)
        {
            if (Count < 1)
            {
                return(null);
            }

            ListX <T> list = new ListX <T>();

            foreach (T item in this)
            {
                if (item == null)
                {
                    continue;
                }
                if (String.Equals((String)GetValue(item, name), value, StringComparison.OrdinalIgnoreCase))
                {
                    list.Add(item);
                }
            }
            if (list == null || list.Count < 1)
            {
                return(null);
            }
            return(list);
        }
Exemple #4
0
        ///// <summary>
        ///// 拥有指定类型转换器的转换
        ///// </summary>
        ///// <param name="collection"></param>
        ///// <param name="func"></param>
        ///// <returns></returns>
        //public static ListX<T> From(IEnumerable collection, Func<Object, T> func)
        //{
        //    if (collection == null || collection.GetEnumerator() == null || collection.GetEnumerator().Current == null) return null;

        //    ListX<T> list = new ListX<T>();
        //    foreach (Object item in collection)
        //    {
        //        if (func == null)
        //            list.Add((T)item);
        //        else
        //            list.Add(func(item));
        //    }
        //    if (list == null || list.Count < 1) return null;
        //    return list;
        //}

        /// <summary>
        /// 拥有指定类型转换器的转换
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="collection"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static ListX <T> From <T2>(IEnumerable collection, ITOrm.Ms.Models.Reflection.Func <T2, T> func)
        {
            if (collection == null || collection.GetEnumerator() == null)
            {
                return(null);
            }

            ListX <T> list = new ListX <T>();

            foreach (T2 item in collection)
            {
                if (item == null)
                {
                    continue;
                }
                T entity = default(T);
                if (func == null)
                {
                    entity = (T)(Object)item;
                }
                else
                {
                    entity = func(item);
                }
                if (entity != null)
                {
                    list.Add(entity);
                }
            }
            if (list == null || list.Count < 1)
            {
                return(null);
            }
            return(list);
        }
Exemple #5
0
        /// <summary>
        /// 检索与指定谓词定义的条件匹配的所有元素。
        /// </summary>
        /// <param name="match">条件</param>
        /// <returns></returns>
        public new ListX <T> FindAll(Predicate <T> match)
        {
            if (Count < 1)
            {
                return(null);
            }

            ListX <T> list = new ListX <T>();

            foreach (T item in this)
            {
                if (item == null)
                {
                    continue;
                }
                if (match(item))
                {
                    list.Add(item);
                }
            }
            if (list == null || list.Count < 1)
            {
                return(null);
            }
            return(list);
        }
Exemple #6
0
        /// <summary>
        /// 根据指定项查找
        /// </summary>
        /// <param name="name">属性名</param>
        /// <param name="value">属性值</param>
        /// <returns></returns>
        public ListX <T> FindAll(String name, Object value)
        {
            if (Count < 1)
            {
                return(null);
            }

            ListX <T> list = new ListX <T>();

            foreach (T item in this)
            {
                if (item == null)
                {
                    continue;
                }
                //if (Object.Equals(GetValue(item, name), value)) list.Add(item);
                if (Object.Equals(GetValue(item, name), value))
                {
                    list.Add(item);
                }
            }
            if (list == null || list.Count < 1)
            {
                return(null);
            }
            return(list);
        }
Exemple #7
0
        /// <summary>
        /// 添加全局方法
        /// </summary>
        /// <param name="method"></param>
        /// <param name="fun"></param>
        public void AddGlobalMethod(MethodInfo method, Action <ILGenerator> fun)
        {
            ParameterInfo[] ps         = method.GetParameters();
            ListX <Type>    paramTypes = ListX <Type> .From <ParameterInfo>(ps, delegate(ParameterInfo item) { return(item.ParameterType); });

            //TypeBuilder tbuilder = mbuilder.DefineType(method.DeclaringType.Name, TypeAttributes.Public);
            //MethodBuilder mb = tbuilder.DefineMethod(method.Name.Replace(".", "_"), method.Attributes, method.ReturnType, paramTypes);
            if (TypeBuilder == null)
            {
                TypeBuilder = ModBuilder.DefineType(Name, TypeAttributes.Public);
            }

            String name = method.Name.Replace(".", "_");

            if (String.IsNullOrEmpty(name))
            {
                name = "Test" + DateTime.Now.Ticks;
            }
            //MethodBuilder mb = ModBuilder.DefineGlobalMethod(name, method.Attributes, method.ReturnType, paramTypes.ToArray());
            MethodBuilder mb = TypeBuilder.DefineMethod(name, method.Attributes, method.ReturnType, paramTypes.ToArray());

            ILGenerator il = mb.GetILGenerator();

            fun(il);
        }
Exemple #8
0
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public virtual void Import(TextReader reader)
        {
            //XmlAttributeOverrides ovs = new XmlAttributeOverrides();
            //foreach (FieldItem item in (this[0] as IEntityOperate).Fields)
            //{
            //    XmlAttributes atts = new XmlAttributes();
            //    atts.XmlAttribute = new XmlAttributeAttribute();
            //    ovs.Add(item.Property.DeclaringType, item.Name, atts);
            //}
            //XmlSerializer serial = new XmlSerializer(this.GetType(), ovs);

            XmlSerializer serial = CreateXmlSerializer();
            ListX <T>     list   = serial.Deserialize(reader) as ListX <T>;

            AddRange(list);
        }
Exemple #9
0
        //static ListX<MemberInfo> GetMembers(Type type)
        //{
        //    ListX<MemberInfo> list = new ListX<MemberInfo>();
        //    MemberInfo[] mis = type.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
        //    if (mis != null && mis.Length > 0) list.AddRange(mis);

        //    if (type.BaseType != null)
        //    {
        //        ListX<MemberInfo> list2 = GetMembers(type.BaseType);
        //        if (list2 != null) list.AddRange(list2);
        //    }

        //    if (list.Count < 1) return null;
        //    return list;
        //}

        ListX <T> GetMembers <T>(MemberTypes memberType) where T : MemberInfo
        {
            if (Members == null || Members.Count < 1)
            {
                return(null);
            }

            ListX <T> list = new ListX <T>();

            foreach (MemberInfo item in Members)
            {
                if (item.MemberType == memberType)
                {
                    list.Add(item as T);
                }
            }
            return(list.Count > 0 ? list : null);
        }
Exemple #10
0
        /// <summary>
        /// 查找插件,带缓存
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ListX <Type> FindPlugins(Type type)
        {
            ListX <Type> list = null;

            if (_plugins.TryGetValue(type, out list))
            {
                return(list);
            }
            lock (_plugins)
            {
                if (_plugins.TryGetValue(type, out list))
                {
                    return(list);
                }

                ListX <TypeX> list2 = TypeXs;
                if (list2 == null || list2.Count < 1)
                {
                    list = null;
                }
                else
                {
                    list = new ListX <Type>();
                    foreach (TypeX item in list2)
                    {
                        if (item.IsPlugin(type))
                        {
                            list.Add(item.Type);
                        }
                    }
                    if (list.Count <= 0)
                    {
                        list = null;
                    }
                }

                _plugins.Add(type, list);

                return(list);
            }
        }
Exemple #11
0
        /// <summary>
        /// 只反射加载有效路径(应用程序是当前路径,Web是Bin目录)的所有程序集
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public static ListX <AssemblyX> ReflectionOnlyGetAssemblies(AppDomain domain)
        {
            if (domain == null)
            {
                domain = AppDomain.CurrentDomain;
            }

            Assembly[] asms = domain.ReflectionOnlyGetAssemblies();
            if (asms == null || asms.Length < 1)
            {
                return(null);
            }

            ListX <AssemblyX> list = new ListX <AssemblyX>();

            foreach (Assembly item in asms)
            {
                list.Add(AssemblyX.Create(item));
            }

            return(list);
        }
Exemple #12
0
        /// <summary>
        /// 集合相减
        /// </summary>
        /// <param name="list1">第一个实体集合</param>
        /// <param name="list2">第二个实体集合</param>
        /// <returns></returns>
        public static ListX <T> operator -(ListX <T> list1, ListX <T> list2)
        {
            if (list1 == null || list1.Count < 1)
            {
                return(null);
            }

            ListX <T> list = new ListX <T>();

            foreach (T item in list1)
            {
                if (list2 != null && !list2.Contains(item))
                {
                    list.Add(item);
                }
            }

            if (list == null || list.Count < 1)
            {
                return(null);
            }

            return(list);
        }
Exemple #13
0
        /// <summary>
        /// 根据名称获取类型
        /// </summary>
        /// <param name="typeName">类型名</param>
        /// <param name="isLoadAssembly">是否从未加载程序集中获取类型。使用仅反射的方法检查目标类型,如果存在,则进行常规加载</param>
        /// <returns></returns>
        public static Type GetType(String typeName, Boolean isLoadAssembly)
        {
            if (String.IsNullOrEmpty(typeName))
            {
                throw new ArgumentNullException("typeName");
            }

            // 基本获取
            Type type = Type.GetType(typeName);

            if (type != null)
            {
                return(type);
            }

            // 尝试本程序集
            Assembly[] asms = new[] {
                Assembly.GetExecutingAssembly(),
                       Assembly.GetCallingAssembly(),
                       Assembly.GetEntryAssembly()
            };

            foreach (Assembly asm in asms)
            {
                if (asm != null)
                {
                    type = asm.GetType(typeName);

                    if (type != null)
                    {
                        return(type);
                    }
                }
            }

            // 尝试所有程序集
            ListX <AssemblyX> list = AssemblyX.GetAssemblies();

            if (list != null && list.Count > 0)
            {
                foreach (AssemblyX asm in list)
                {
                    type = asm.Asm.GetType(typeName);
                    if (type != null)
                    {
                        return(type);
                    }
                }
            }

            // 尝试加载程序集
            AssemblyX.ReflectionOnlyLoad();
            list = AssemblyX.ReflectionOnlyGetAssemblies();
            if (list != null && list.Count > 0)
            {
                foreach (AssemblyX asm in list)
                {
                    type = asm.Asm.GetType(typeName);
                    if (type != null)
                    {
                        // 真实加载
                        Assembly asm2  = Assembly.LoadFile(asm.Asm.Location);
                        Type     type2 = asm2.GetType(typeName);
                        if (type2 != null)
                        {
                            type = type2;
                        }

                        return(type);
                    }
                }
            }

            return(null);
        }
Exemple #14
0
        /// <summary>
        /// 只反射加载指定路径的所有程序集
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static ListX <AssemblyX> ReflectionOnlyLoad(String path)
        {
            if (!Directory.Exists(path))
            {
                return(null);
            }

            String[] ss = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
            if (ss == null || ss.Length < 1)
            {
                return(null);
            }

            //AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);

            //try
            {
                ListX <AssemblyX> loadeds  = AssemblyX.GetAssemblies();
                ListX <AssemblyX> loadeds2 = AssemblyX.ReflectionOnlyGetAssemblies();

                ListX <AssemblyX> list = new ListX <AssemblyX>();
                foreach (String item in ss)
                {
                    if (!item.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) &&
                        !item.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    if (loadeds != null && loadeds.Exists(delegate(AssemblyX elm)
                    {
                        return(item.Equals(elm.Asm.Location, StringComparison.OrdinalIgnoreCase));
                    }))
                    {
                        continue;
                    }

                    if (loadeds2 != null && loadeds2.Exists(delegate(AssemblyX elm)
                    {
                        return(item.Equals(elm.Asm.Location, StringComparison.OrdinalIgnoreCase));
                    }))
                    {
                        continue;
                    }

                    try
                    {
                        //Assembly asm = Assembly.ReflectionOnlyLoad(File.ReadAllBytes(item));
                        Assembly asm = Assembly.ReflectionOnlyLoadFrom(item);

                        list.Add(AssemblyX.Create(asm));
                    }
                    catch { }
                }

                return(list);
            }
            //finally
            //{
            //    AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve -= new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
            //}
        }