コード例 #1
0
ファイル: DbClassInfoCache.cs プロジェクト: radtek/DataAccess
        <DbPropertyInfoCache, DbAttributeInfoCache, DbMethodInfoCache, DbConstructorInfoCache, DbMethodArgument> Init(
            Type type, bool anon = false)
        {
            var item = base.Init(type, anon);

            //.ToDictionary(s => s.Key);
            Propertys = new Dictionary <string, DbPropertyInfoCache>(Propertys
                                                                     .Where(f => f.Value.Attributes.All(e => !(e.Attribute is IgnoreReflectionAttribute)))
                                                                     .ToDictionary(s => s.Key, f => f.Value));
            Mehtods =
                new HashSet <DbMethodInfoCache>(
                    Mehtods.Where(f => f.Attributes.All(d => !(d.Attribute is IgnoreReflectionAttribute))));
            Constructors =
                new HashSet <DbConstructorInfoCache>(
                    Constructors.Where(f => f.Attributes.All(e => !(e.Attribute is IgnoreReflectionAttribute))));
            foreach (var dbPropertyInfoCach in Propertys)
            {
                dbPropertyInfoCach.Value.DeclaringClass = this;
            }
            foreach (var dbPropertyInfoCach in Mehtods)
            {
                dbPropertyInfoCach.DeclaringClass = this;
            }
            foreach (var dbPropertyInfoCach in Constructors)
            {
                dbPropertyInfoCach.DeclaringClass = this;
            }

            Refresh(true);
            return(item);
        }
コード例 #2
0
ファイル: ClassInfoCache.cs プロジェクト: JPVenson/DataAccess
        public virtual IClassInfoCache <TProp, TAttr, TMeth, TCtor, TArg> Init(Type type, bool anon = false)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (!String.IsNullOrEmpty(Name))
            {
                throw new InvalidOperationException("The object is already Initialed. A Change is not allowed");
            }

            Name = type.FullName;
            Type = type;

            if (Type.IsInterface)
            {
                anon = true;
            }

            foreach (var attributeInfoCach in type
                     .GetCustomAttributes(true)
                     .Where(s => s is Attribute)
                     .Select(s => new TAttr().Init(s as Attribute) as TAttr))
            {
                Attributes.Add(attributeInfoCach);
            }
            foreach (var propertyInfoCach in type
                     .GetProperties(BindingFlags.Public | BindingFlags.Static |
                                    BindingFlags.NonPublic | BindingFlags.Instance)
                     .Where(e => !e.GetIndexParameters().Any())
                     .Select(s => new TProp().Init(s, anon) as TProp)
                     .ToDictionary(s => s.PropertyName, s => s))
            {
                Propertys.Add(propertyInfoCach.Key, propertyInfoCach.Value);
            }

            foreach (var methodInfoCach in type
                     .GetMethods(BindingFlags.Public | BindingFlags.Static |
                                 BindingFlags.NonPublic | BindingFlags.Instance)
                     .Select(s => new TMeth().Init(s) as TMeth))
            {
                Mehtods.Add(methodInfoCach);
            }

            foreach (var constructor in type
                     .GetConstructors(BindingFlags.Public | BindingFlags.Static |
                                      BindingFlags.NonPublic | BindingFlags.Instance)
                     .Select(s => new TCtor().Init(s) as TCtor))
            {
                Constructors.Add(constructor);
            }
            var defaultConstructor = Constructors.FirstOrDefault(f => !f.Arguments.Any());

            IsMsCoreFrameworkType = type.Assembly == MsCoreLibAssembly;

            //if (type.IsValueType)
            //{
            //	var dm = new DynamicMethod("InvokeDefaultCtorFor" + ClassName, Type, System.Type.EmptyTypes, Type, true);
            //	var il = dm.GetILGenerator();
            //	il.Emit(OpCodes.Initobj, type);
            //	var ctorDelegate = dm.CreateDelegate(typeof(Func<>).MakeGenericType(type));
            //	DefaultFactory = new TMeth().Init(ctorDelegate.Method, type) as TMeth;
            //}
            //else if (defaultConstructor != null)
            //{
            //	var dm = new DynamicMethod("InvokeDefaultCtorFor" + ClassName, Type, System.Type.EmptyTypes, Type, true);
            //	var il = dm.GetILGenerator();
            //	il.Emit(OpCodes.Newobj);
            //	var ctorDelegate = dm.CreateDelegate(typeof(Func<>).MakeGenericType(type));
            //	DefaultFactory = new TMeth().Init(ctorDelegate.Method, type) as TMeth;
            //}

            if (type.IsValueType || defaultConstructor != null)
            {
                Expression defaultExpression = null;

                if (type.IsValueType)
                {
                    defaultExpression = Expression.Default(type);
                }
                else if (defaultConstructor != null)
                {
                    defaultExpression = Expression.New(defaultConstructor.MethodInfo as ConstructorInfo);
                }

                var expressionInvoker = MetaInfoStoreExtentions.GetExpressionLambda();

                var createLambda = expressionInvoker
                                   .MakeGenericMethod(typeof(Func <>).MakeGenericType(type))
                                   .Invoke(null, new object[] { defaultExpression, new List <ParameterExpression>() });

                DefaultFactory = MetaInfoStoreExtentions.GetCompileMethodFromExpression(createLambda.GetType())
                                 .Invoke(createLambda, null);
            }

            return(this);
        }