Example #1
0
        public static T CreateMapper <T>(MapperProvider provider = null)
            where T : IMapper
        {
            Type type = typeof(T);
            Type item = null;

            if (!BaseMapper.mapperTypes.TryGetValue(type, out item))
            {
                BaseMapper.LoadAssembliy(type.GetTypeInfo().Assembly);
                if (!BaseMapper.mapperTypes.ContainsKey(type))
                {
                    throw new Exception(string.Concat(type.FullName, " not exist mapper concrete"));
                }
                item = BaseMapper.mapperTypes[type];
            }
            T t = (T)Activator.CreateInstance(item);

            t.Provider = provider ?? MapperProvider.DefaultProvider;
            return(t);
        }
Example #2
0
 public static void LoadAssembliy(Assembly assembly)
 {
     BaseMapper.logger.Debug(string.Concat("LoadAssembliy ", assembly.FullName), (LogInfo)null, null);
     foreach (Type type1 in (
                  from type in (IEnumerable <Type>)assembly.GetTypes()
                  select new { type = type, typeInfo = type.GetTypeInfo() }).Where((argument0) => {
         if (!argument0.typeInfo.ImplementedInterfaces.Contains <Type>(typeof(IMapper)))
         {
             return(false);
         }
         return(!argument0.typeInfo.IsSubclassOf(typeof(BaseMapper)));
     }).Select((argument1) => argument1.type))
     {
         if (BaseMapper.mapperTypes.ContainsKey(type1))
         {
             continue;
         }
         Type type2 = BaseMapper.BuildMapperAssembly(type1);
         BaseMapper.mapperTypes.Add(type1, type2);
     }
 }
Example #3
0
 internal MapperCombineScope(BaseMapper mapper)
 {
     this.mapper = mapper;
 }
Example #4
0
 public static T CreateMapper <T>(string provider)
     where T : IMapper
 {
     return(BaseMapper.CreateMapper <T>(MapperProvider.CreateProvider(provider)));
 }
Example #5
0
        internal static Type BuildMapperAssembly(Type mapperInterfaceType)
        {
            object name;
            object obj;

            BaseMapper.logger.Debug(string.Concat("BuildMapperAssembly ", mapperInterfaceType.FullName), (LogInfo)null, null);
            string          str             = string.Concat(mapperInterfaceType.Namespace, ".Concrete");
            AppDomain       currentDomain   = AppDomain.CurrentDomain;
            AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(str), AssemblyBuilderAccess.Run);
            string          str1            = string.Concat(str, ".", mapperInterfaceType.Name.Substring(1));
            ModuleBuilder   moduleBuilder   = assemblyBuilder.DefineDynamicModule(str1);
            TypeBuilder     typeBuilder     = moduleBuilder.DefineType(str1, TypeAttributes.Public, BaseMapper.typeBaseMapper);
            NameAttribute   customAttribute = mapperInterfaceType.GetTypeInfo().GetCustomAttribute <NameAttribute>();

            if (customAttribute != null)
            {
                name = customAttribute.Name;
            }
            else
            {
                name = null;
            }
            if (name == null)
            {
                name = mapperInterfaceType.Name.Substring(1, mapperInterfaceType.Name.Length - "IMapper".Length);
            }
            string str2 = (string)name;

            typeBuilder.AddInterfaceImplementation(mapperInterfaceType);
            AnonymousTypeBuilder anonymousTypeBuilder = new AnonymousTypeBuilder(assemblyBuilder, moduleBuilder);

            MethodInfo[] methods = mapperInterfaceType.GetMethods();
            for (int i = 0; i < (int)methods.Length; i++)
            {
                MethodInfo      methodInfo = methods[i];
                ParameterInfo[] parameters = methodInfo.GetParameters();
                Type[]          array      = (
                    from p in (IEnumerable <ParameterInfo>)methodInfo.GetParameters()
                    select p.ParameterType).ToArray <Type>();
                List <ParameterAttribute> list          = methodInfo.GetCustomAttributes <ParameterAttribute>().ToList <ParameterAttribute>();
                MethodBuilder             methodBuilder = typeBuilder.DefineMethod(methodInfo.Name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.VtableLayoutMask | MethodAttributes.NewSlot, CallingConventions.Standard, methodInfo.ReturnType, array);
                for (byte j = 0; j < (int)array.Length; j = (byte)(j + 1))
                {
                    methodBuilder.DefineParameter(j + 1, ParameterAttributes.None, parameters[j].Name);
                }
                ILGenerator lGenerator = methodBuilder.GetILGenerator();
                if (methodInfo.ReturnType != typeof(void))
                {
                    lGenerator.DeclareLocal(methodInfo.ReturnType);
                }
                lGenerator.Emit(OpCodes.Nop);
                lGenerator.Emit(OpCodes.Ldarg_0);
                lGenerator.Emit(OpCodes.Call, BaseMapper.typeBaseMapper.GetMethod("get_Provider", BindingFlags.Instance | BindingFlags.Public));
                NameAttribute nameAttribute = methodInfo.GetCustomAttribute <NameAttribute>();
                if (nameAttribute != null)
                {
                    obj = nameAttribute.Name;
                }
                else
                {
                    obj = null;
                }
                if (obj == null)
                {
                    obj = string.Concat(str2, ".", methodInfo.Name);
                }
                string str3 = (string)obj;
                lGenerator.Emit(OpCodes.Ldstr, str3);
                if (array.Length != 0 || list.Count != 0)
                {
                    sbyte  num       = 0;
                    bool[] flagArray = new bool[(int)array.Length + list.Count];
                    for (byte k = 0; k < (int)array.Length; k = (byte)(k + 1))
                    {
                        flagArray[k] = array[k].GetTypeInfo().IsSubclassOf(typeof(ValueObject));
                        if (flagArray[k])
                        {
                            num = (sbyte)(num + 1);
                        }
                    }
                    for (byte l = 0; l < list.Count; l = (byte)(l + 1))
                    {
                        flagArray[(int)array.Length + l] = list[l].Parameter.Value.GetType().GetTypeInfo().IsSubclassOf(typeof(ValueObject));
                        if (flagArray[(int)array.Length + l])
                        {
                            num = (sbyte)(num + 1);
                        }
                    }
                    Type[]   type          = new Type[(int)array.Length + list.Count - num];
                    string[] parameterName = new string[(int)array.Length + list.Count - num];
                    BaseMapper.EmitHepler(lGenerator, OpCodes.Ldc_I4_S, num + (type.Length == 0 ? 0 : 1));
                    lGenerator.Emit(OpCodes.Newarr, typeof(object));
                    lGenerator.Emit(OpCodes.Dup);
                    sbyte num1 = 0;
                    for (byte m = 0; m < (int)array.Length; m = (byte)(m + 1))
                    {
                        if (!flagArray[m])
                        {
                            type[m - num1]          = array[m];
                            parameterName[m - num1] = parameters[m].Name;
                        }
                        else
                        {
                            OpCode ldcI4S = OpCodes.Ldc_I4_S;
                            sbyte  num2   = num1;
                            num1 = (sbyte)(num2 + 1);
                            BaseMapper.EmitHepler(lGenerator, ldcI4S, num2);
                            BaseMapper.EmitHepler(lGenerator, OpCodes.Ldarg_S, m + 1);
                            lGenerator.Emit(OpCodes.Stelem_Ref);
                            if (m == (int)array.Length - (type.Length == 0 ? 2 : 1))
                            {
                                lGenerator.Emit(OpCodes.Dup);
                            }
                        }
                    }
                    for (byte n = 0; n < list.Count; n = (byte)(n + 1))
                    {
                        if (!flagArray[(int)array.Length + n])
                        {
                            BaseMapper.directParameterValue.Add(string.Concat(str1, ".", methodInfo.Name), list[n].Parameter.Value);
                            type[(int)array.Length + n - num1]          = list[n].Parameter.Value.GetType();
                            parameterName[(int)array.Length + n - num1] = list[n].Parameter.ParameterName;
                        }
                    }
                    if (type.Length != 0)
                    {
                        BaseMapper.EmitHepler(lGenerator, OpCodes.Ldc_I4_S, num);
                        for (byte o = 0; o < (int)array.Length; o = (byte)(o + 1))
                        {
                            if (!flagArray[o])
                            {
                                BaseMapper.EmitHepler(lGenerator, OpCodes.Ldarg_S, o + 1);
                            }
                        }
                        for (byte p1 = 0; p1 < list.Count; p1 = (byte)(p1 + 1))
                        {
                            if (!flagArray[(int)array.Length + p1])
                            {
                                lGenerator.Emit(OpCodes.Ldsfld, BaseMapper.fieldDirectParameterValue);
                                lGenerator.Emit(OpCodes.Ldstr, string.Concat(str1, ".", methodInfo.Name));
                                lGenerator.Emit(OpCodes.Callvirt, BaseMapper.methodDirectParameterValue);
                            }
                        }
                        Type type1 = anonymousTypeBuilder.CreateType(type, parameterName);
                        lGenerator.Emit(OpCodes.Newobj, type1.GetConstructors()[0]);
                        lGenerator.Emit(OpCodes.Stelem_Ref);
                    }
                }
                else
                {
                    MethodInfo method = typeof(Array).GetMethod("Empty");
                    method = method.MakeGenericMethod(new Type[] { typeof(object) });
                    lGenerator.Emit(OpCodes.Call, method);
                }
                CommandItem item = CommandRepository.Instance[str3] ?? new CommandItem()
                {
                    Verb = CommandVerb.Get
                };
                MethodInfo methodInfo1 = (methodInfo.ReturnType == typeof(void) || item.Verb != CommandVerb.Get ? BaseMapper.methodQuery : BaseMapper.methodScalar);
                TypeInfo   typeInfo    = methodInfo.ReturnType.GetTypeInfo();
                bool       flag        = typeInfo.ImplementedInterfaces.Contains <Type>(typeof(IList));
                bool       flag1       = typeInfo.ImplementedInterfaces.Contains <Type>(typeof(IDictionary));
                bool       flag2       = typeInfo.IsSubclassOf(typeof(ValueObject));
                bool       flag3       = false;
                if (methodInfo.ReturnType == typeof(ValueSet))
                {
                    methodInfo1 = BaseMapper.methodDataSet;
                }
                else if (methodInfo.ReturnType == typeof(ValueTable))
                {
                    methodInfo1 = BaseMapper.methodDataTable;
                }
                else if (methodInfo.ReturnType == typeof(ValueRow))
                {
                    methodInfo1 = BaseMapper.methodDataRow;
                }
                else if (item.Verb == CommandVerb.Get && flag | flag1 | flag2)
                {
                    methodInfo1 = BaseMapper.methodDataTable;
                    flag3       = true;
                }
                if (methodInfo1 == BaseMapper.methodScalar)
                {
                    methodInfo1 = methodInfo1.MakeGenericMethod(new Type[] { methodInfo.ReturnType });
                }
                lGenerator.Emit(OpCodes.Call, methodInfo1);
                if (flag3)
                {
                    MethodInfo methodInfo2 = null;
                    if (!flag)
                    {
                        methodInfo2 = (!flag1 ? BaseMapper.methodToFirstVo.MakeGenericMethod(new Type[] { methodInfo.ReturnType }) : BaseMapper.methodToDictionary.MakeGenericMethod(new Type[] { methodInfo.ReturnType.GenericTypeArguments[0], methodInfo.ReturnType.GenericTypeArguments[1] }));
                    }
                    else
                    {
                        methodInfo2 = BaseMapper.methodToList.MakeGenericMethod(new Type[] { methodInfo.ReturnType.GenericTypeArguments[0] });
                    }
                    lGenerator.Emit(OpCodes.Call, methodInfo2);
                }
                if (methodInfo.ReturnType != typeof(void))
                {
                    lGenerator.Emit(OpCodes.Stloc_0);
                    lGenerator.Emit(OpCodes.Br_S, (sbyte)0);
                    lGenerator.Emit(OpCodes.Ldloc_0);
                }
                else
                {
                    lGenerator.Emit(OpCodes.Pop);
                }
                lGenerator.Emit(OpCodes.Ret);
            }
            return(typeBuilder.CreateTypeInfo().AsType());
        }
Example #6
0
        public static void CreateMapperMember(object model)
        {
            Type type = model.GetType();
            List <BaseMapperService.MapperMemberInfo> mapperMemberInfos = null;

            if (!BaseMapperService.mapperMemberInfo.TryGetValue(type, out mapperMemberInfos))
            {
                lock (BaseMapperService._lock)
                {
                    if (!BaseMapperService.mapperMemberInfo.TryGetValue(type, out mapperMemberInfos))
                    {
                        mapperMemberInfos = new List <BaseMapperService.MapperMemberInfo>();
                        FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                        for (int i = 0; i < (int)fields.Length; i++)
                        {
                            FieldInfo fieldInfo = fields[i];
                            Type      fieldType = fieldInfo.FieldType;
                            TypeInfo  typeInfo  = fieldType.GetTypeInfo();
                            if (typeInfo.ImplementedInterfaces.Contains <Type>(typeof(IMapper)) && !typeInfo.IsSubclassOf(typeof(BaseMapper)) && fieldInfo.GetValue(model) == null)
                            {
                                List <ProviderParameterAttribute> list                        = fieldType.GetCustomAttributes <ProviderParameterAttribute>().ToList <ProviderParameterAttribute>();
                                MapperProviderAttribute           customAttribute             = fieldInfo.GetCustomAttribute <MapperProviderAttribute>();
                                List <ProviderParameterAttribute> providerParameterAttributes = fieldInfo.GetCustomAttributes <ProviderParameterAttribute>().ToList <ProviderParameterAttribute>();
                                foreach (ProviderParameterAttribute providerParameterAttribute in list)
                                {
                                    if (providerParameterAttributes.Exists((ProviderParameterAttribute p) => p.Name == providerParameterAttribute.Name))
                                    {
                                        continue;
                                    }
                                    providerParameterAttributes.Add(providerParameterAttribute);
                                }
                                if (customAttribute != null)
                                {
                                    MapperProvider mapperProvider = MapperProvider.CreateProvider(customAttribute.Name);
                                    mapperMemberInfos.Add(new BaseMapperService.MapperMemberInfo()
                                    {
                                        Field     = fieldInfo,
                                        Type      = fieldType,
                                        Parameter = list,
                                        Provider  = mapperProvider
                                    });
                                }
                                else
                                {
                                    mapperMemberInfos.Add(new BaseMapperService.MapperMemberInfo()
                                    {
                                        Field     = fieldInfo,
                                        Type      = fieldType,
                                        Parameter = list
                                    });
                                }
                            }
                        }
                        BaseMapperService.mapperMemberInfo.Add(type, mapperMemberInfos);
                    }
                }
            }
            foreach (BaseMapperService.MapperMemberInfo mapperMemberInfo in mapperMemberInfos)
            {
                IMapper mapper = BaseMapper.CreateMapper(mapperMemberInfo.Type, mapperMemberInfo.Provider);
                mapperMemberInfo.Field.SetValue(model, mapper);
            }
        }
Example #7
0
 public static void Initialize(IEnumerable <Assembly> assemblies, string path)
 {
     CommandRepository.CreateRepository(path);
     MapperProvider.SearchMapperProvider(assemblies.Concat <Assembly>((IEnumerable <Assembly>)(new Assembly[] { typeof(MapperProvider).GetTypeInfo().Assembly })));
     BaseMapper.Initialize(assemblies);
 }