示例#1
0
        public static SetterDelegate SetterFunc(ClassStruction model, PropertyInfo info)
        {
            if (info.GetSetMethod(true) == null || info.GetSetMethod(true).IsPrivate)
            {
                return(null);
            }
            return((SetterDelegate)(EHandler.CreateMethod <object, object, ENull>((til) =>
            {
                LocalBuilder builder = null;
                if (!info.GetSetMethod(true).IsStatic)
                {
                    builder = til.DeclareLocal(model.TypeHandler);
                    til.REmit(OpCodes.Ldarg_0);
                    til.UnPacket(model.TypeHandler);
                    til.REmit(OpCodes.Stloc_S, builder.LocalIndex);
                }

                EModel localModel = EModel.CreateModelFromBuilder(builder, model.TypeHandler);
                localModel.SProperty(info.Name, () =>
                {
                    til.REmit(OpCodes.Ldarg_1);
                    til.UnPacket(info.PropertyType);
                });
            }, "Setter " + info.DeclaringType.Name + " " + info.Name).Compile(typeof(SetterDelegate))));
        }
示例#2
0
        public static void Create(Type type)
        {
            ClassStruction model = EModel.CreateModelFromAction(null, type).Struction;

            #region GetMethod
            Dictionary <string, GetterDelegate> GetDict = new Dictionary <string, GetterDelegate>();
            foreach (var item in model.Properties)
            {
                GetDict[item.Key] = GetterFunc(model, item.Value);
            }
            foreach (var item in model.Fields)
            {
                GetDict[item.Key] = GetterFunc(model, item.Value);
            }
            GetMethodDict[type] = GetDict;
            #endregion

            #region SetMethod
            Dictionary <string, SetterDelegate> SetDict = new Dictionary <string, SetterDelegate>();
            foreach (var item in model.Properties)
            {
                SetDict[item.Key] = SetterFunc(model, item.Value);
            }
            foreach (var item in model.Fields)
            {
                SetDict[item.Key] = SetterFunc(model, item.Value);
            }
            SetMethodDict[type] = SetDict;
            #endregion
        }
示例#3
0
        public static GetterDelegate GetterFunc(ClassStruction model, FieldInfo info)
        {
            return((GetterDelegate)(EHandler.CreateMethod <object, object>((til) =>
            {
                LocalBuilder builder = null;
                if (!info.IsStatic)
                {
                    builder = til.DeclareLocal(model.TypeHandler);
                    til.REmit(OpCodes.Ldarg_0);
                    til.UnPacket(model.TypeHandler);
                    til.REmit(OpCodes.Stloc_S, builder.LocalIndex);
                }

                EModel localModel = EModel.CreateModelFromBuilder(builder, model.TypeHandler);
                localModel.LFieldValue(info.Name).Packet();
            }, "Getter " + info.DeclaringType.Name + " " + info.Name).Compile(typeof(GetterDelegate))));
        }
示例#4
0
        public static GetterDelegate GetterFunc(ClassStruction model, PropertyInfo info)
        {
            MethodInfo getter = info.GetGetMethod(true);

            if (getter == null || getter.IsPrivate || getter.GetParameters().Length > 0)
            {
                return(null);
            }
            return((GetterDelegate)(EHandler.CreateMethod <object, object>((til) =>
            {
                LocalBuilder builder = null;
                if (!getter.IsStatic)
                {
                    builder = til.DeclareLocal(model.TypeHandler);
                    til.REmit(OpCodes.Ldarg_0);
                    til.UnPacket(model.TypeHandler);
                    til.REmit(OpCodes.Stloc_S, builder.LocalIndex);
                }
                EModel localModel = EModel.CreateModelFromBuilder(builder, model.TypeHandler);
                localModel.LPropertyValue(info.Name).Packet();
            }, "Getter " + info.DeclaringType.Name + " " + info.Name).Compile(typeof(GetterDelegate))));
        }
示例#5
0
        public TypeStructionAnalyzer(Type parameter_Handler) : base()
        {
            PrewCallOption    = LinkCallOption.Default;
            CurrentCallOption = LinkCallOption.Default;
            if (parameter_Handler == null)
            {
                return;
            }
            TypeHandler   = parameter_Handler;
            MethodHandler = parameter_Handler;
            //判断是否为结构体

            if (TypeHandler.IsValueType && !TypeHandler.IsPrimitive && !TypeHandler.IsEnum)
            {
                IsStruct = true;
            }
            else
            {
                IsStruct = false;
            }
            ElementType = TypeHandler.GetElementType();
            if (ElementType != null)
            {
                if (ElementType.IsValueType && !ElementType.IsPrimitive && !ElementType.IsEnum)
                {
                    IsStruct = true;
                }
                else
                {
                    IsStruct = false;
                }
            }
            if (ElementType == null)
            {
                ElementType = TypeHandler;
            }
            //填充类结构缓冲
            if (ClassCache.ClassInfoDict.ContainsKey(TypeHandler.Name))
            {
                Struction = ClassCache.ClassInfoDict[TypeHandler.Name];
            }
            else
            {
                //创建类结构
                Struction             = new ClassStruction();
                Struction.Name        = TypeHandler.Name;
                Struction.TypeHandler = TypeHandler;
                Struction.IsStruct    = IsStruct;
                ClassCache.ClassInfoDict[TypeHandler.Name] = Struction;

                Type[]         types     = TypeHandler.GetInterfaces();
                HashSet <Type> hashTypes = new HashSet <Type>(types);

                #region 此处保留,因为具体操作的时候有些接口是没必要用的,可以在这里做过滤
                Type type = TypeHandler.GetInterface("IDictionary");
                if (type != null)
                {
                    for (int i = 0; i < types.Length; i += 1)
                    {
                        if (types[i].Name.Contains("ICollection") || !types[i].IsGenericType)
                        {
                            hashTypes.Remove(types[i]);
                        }
                    }
                }
                #endregion

                hashTypes.Add(TypeHandler);
                foreach (var item in hashTypes)
                {
                    Type tempType = item;

                    //获取方法结构
                    MethodInfo[] methods = tempType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    for (int j = 0; j < methods.Length; j += 1)
                    {
                        string methodName = methods[j].Name;
                        if (methodName.Length > 4)
                        {
                            if (methodName[0] == 103 || methodName[0] == 115)
                            {
                                if (methodName[1] == 101)
                                {
                                    if (methodName[2] == 116)
                                    {
                                        if (methodName[3] == 95)
                                        {
                                            continue;
                                        }
                                    }
                                }
                            }
                        }

                        Struction.Methods[methods[j].Name] = methods[j];
                        //处理方法标签
                        object[] attributes = methods[j].GetCustomAttributes(true);
                        if (attributes != null && attributes.Length > 0)
                        {
                            Struction.AttributeTree[methods[j].Name] = new Dictionary <string, object>();
                            for (int n = 0; n < attributes.Length; n += 1)
                            {
                                Type attributeType = attributes[n].GetType();
                                if (CheckHaveDefautlConstructor(attributeType))
                                {
                                    Struction.AttributeTree[methods[j].Name][attributeType.Name] = attributes[n];
                                }
                            }
                        }
                    }
                    Type currentType = null;
                    //获取属性结构
                    PropertyInfo[] properties = tempType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    for (int j = 0; j < properties.Length; j += 1)
                    {
                        currentType = properties[j].PropertyType;
                        //内部私有类的数组不做操作
                        if (currentType.IsArray)
                        {
                            if (currentType.GetElementType().GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (currentType.GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        //处理属性标签
                        object[] attributes = properties[j].GetCustomAttributes(true);
                        if (attributes != null && attributes.Length > 0)
                        {
                            Struction.AttributeTree[properties[j].Name] = new Dictionary <string, object>();
                            for (int n = 0; n < attributes.Length; n += 1)
                            {
                                Type attributeType = attributes[n].GetType();
                                if (CheckHaveDefautlConstructor(attributeType))
                                {
                                    Struction.AttributeTree[properties[j].Name][attributeType.Name] = attributes[n];
                                }
                            }
                        }
                        Struction.Properties[properties[j].Name] = properties[j];

                        //if (currentType.IsClass && ((currentType.BaseType == typeof(MulticastDelegate) || currentType.BaseType == typeof(Delegate))))
                        //{
                        //    Struction.DelegateMethods[properties[j].Name] = currentType.GetMethod("Method");
                        //}
                    }

                    //获取字段结构
                    FieldInfo[] fields = tempType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    for (int j = 0; j < fields.Length; j += 1)
                    {
                        currentType = fields[j].FieldType;
                        //内部私有类的数组不做操作
                        if (currentType.IsArray)
                        {
                            if (currentType.GetElementType().GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            //readonly const 内部私有类 不做操作
                            if (fields[j].IsInitOnly || (fields[j].IsLiteral && fields[j].IsStatic) || currentType.GetTypeInfo().IsNestedPrivate)
                            {
                                continue;
                            }
                        }
                        //处理字段标签
                        object[] attributes = fields[j].GetCustomAttributes(true);
                        if (attributes != null && attributes.Length > 0)
                        {
                            Struction.AttributeTree[fields[j].Name] = new Dictionary <string, object>();
                            for (int n = 0; n < attributes.Length; n += 1)
                            {
                                Type attributeType = attributes[n].GetType();
                                if (CheckHaveDefautlConstructor(attributeType))
                                {
                                    Struction.AttributeTree[fields[j].Name][attributeType.Name] = attributes[n];
                                }
                            }
                        }
                        Struction.Fields[fields[j].Name] = fields[j];

                        //if (currentType.IsClass && (currentType.BaseType == typeof(MulticastDelegate) || currentType.BaseType == typeof(Delegate)))
                        //{
                        //    //PropertyInfo info = currentType.GetProperty("Method");
                        //    //info.GetValue();
                        //    //Struction.DelegateMethods[fields[j].Name] =
                        //}
                    }
                }
            }
            if (this is IDelayOperator)
            {
                ((IDelayOperator)this).Initialize();
            }
        }
示例#6
0
文件: EClone.cs 项目: wcfylcf/Natasha
        /// <summary>
        /// 深度复制对象,并压入栈中,创建一个新的临时变量
        /// </summary>
        /// <param name="value">类或者结构体或者数组</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static LocalBuilder GetCloneBuilder(object value, Type type)
        {
            ILGenerator il = ThreadCache.GetIL();

            if (il.IsNullable(type))
            {
                LocalBuilder builder = il.DeclareLocal(type);
                if (value == null)
                {
                    il.REmit(OpCodes.Ldloca_S, builder);
                    il.InitObject(type);
                }
                else
                {
                    il.LoadObject(value, value.GetType());
                    il.CallNullableCtor(type);
                }
                return(builder);
            }
            else if (value == null)
            {
                LocalBuilder builder = il.DeclareLocal(type);
                il.REmit(OpCodes.Ldnull);
                il.REmit(OpCodes.Stloc_S, builder.LocalIndex);
                return(builder);
            }
            else if (type.IsValueType && !type.IsPrimitive)
            {
                LocalBuilder builder = il.DeclareLocal(type);
                if (type.IsEnum)
                {
                    il.EmitInt((int)value);
                    il.REmit(OpCodes.Stloc_S, builder);
                    return(builder);
                }
                if (EStruckCheck.IsDefaultStruct(value, type))
                {
                    EModel structModel = EModel.CreateModel(type).UseDefaultConstructor();
                    return(structModel.Builder);
                }
            }
            else if (type.IsArray)
            {
                Array  tempArray    = (Array)value;
                Type   instanceType = value.GetType().GetElementType();
                EArray array        = EArray.CreateArraySpecifiedLength(value.GetType(), tempArray.Length);
                for (int i = 0; i < tempArray.Length; i += 1)
                {
                    object result = tempArray.GetValue(i);

                    if (result != null)
                    {
                        if (array.IsStruct && !array.ElementType.IsEnum)
                        {
                            if (EStruckCheck.IsDefaultStruct(result, instanceType))
                            {
                                continue;
                            }
                        }
                        array.StoreArray(i, result);
                    }
                }
                return(array.Builder);
            }
            if (!EReflector.GetMethodDict.ContainsKey(type))
            {
                EReflector.Create(type);
            }

            Dictionary <string, GetterDelegate> GetDict = EReflector.GetMethodDict[type];
            ClassStruction struction = ClassCache.ClassInfoDict[type.Name];
            EModel         model     = EModel.CreateModel(type).UseDefaultConstructor();

            //foreach (var item in struction.Properties)
            //{
            //    MethodInfo info = item.Value.GetSetMethod(true);
            //    if (info == null || GetDict[item.Key] == null || info.IsPrivate || info.IsStatic)
            //    {
            //        continue;
            //    }

            //    object result = GetDict[item.Key](value);
            //    if (result == null)
            //    {
            //        continue;
            //    }
            //    model.SProperty(item.Key, result);
            //}

            foreach (var item in struction.Fields)
            {
                if (item.Value.IsStatic)
                {
                    continue;
                }
                object result = GetDict[item.Key](value);
                if (result == null)
                {
                    continue;
                }
                model.SField(item.Key, result);
            }
            return(model.Builder);
        }