/// <summary> /// Create a new instance of the specified type /// </summary> /// <returns></returns> public static CreateObject CreateObjectFactory <T>() where T : class { Type t = typeof(T); FastObjectFactory.CreateObject c = creatorCache[t] as FastObjectFactory.CreateObject; if (c == null) { lock (creatorCache.SyncRoot) { c = creatorCache[t] as FastObjectFactory.CreateObject; if (c != null) { return(c); } DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + t.Name, typeof(object), null, t); ILGenerator ilGen = dynMethod.GetILGenerator(); ilGen.Emit(OpCodes.Newobj, t.GetConstructor(Type.EmptyTypes)); ilGen.Emit(OpCodes.Ret); c = (CreateObject)dynMethod.CreateDelegate(coType); creatorCache.Add(t, c); } } return(c); }
// Token: 0x0600158D RID: 5517 RVA: 0x000CFDB0 File Offset: 0x000CDFB0 public static FastObjectFactory.CreateObject CreateObjectFactory(Type t) { FastObjectFactory.CreateObject createObject = FastObjectFactory.hashtable_0[t] as FastObjectFactory.CreateObject; if (createObject == null) { object syncRoot = FastObjectFactory.hashtable_0.SyncRoot; FastObjectFactory.CreateObject result; lock (syncRoot) { createObject = (FastObjectFactory.hashtable_0[t] as FastObjectFactory.CreateObject); if (createObject != null) { result = createObject; } else { DynamicMethod dynamicMethod = new DynamicMethod("DM$OBJ_FACTORY_" + t.Name, t, new Type[] { typeof(IntPtr) }, true); ILGenerator ilgenerator = dynamicMethod.GetILGenerator(); ilgenerator.Emit(OpCodes.Ldarg_0); ConstructorInfo constructor = t.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(IntPtr) }, null); if (!(constructor == null)) { ilgenerator.Emit(OpCodes.Newobj, constructor); ilgenerator.Emit(OpCodes.Ret); createObject = (FastObjectFactory.CreateObject)dynamicMethod.CreateDelegate(FastObjectFactory.type_0); FastObjectFactory.hashtable_0.Add(t, createObject); return(createObject); } result = null; } } return(result); } return(createObject); }
private static AdapterModel <TSource, TDestination> CreateAdapterModel() { FastObjectFactory.CreateObject fieldModelFactory = FastObjectFactory.CreateObjectFactory <FieldModel>(); FastObjectFactory.CreateObject propertyModelFactory = FastObjectFactory.CreateObjectFactory <PropertyModel <TSource, TDestination> >(); FastObjectFactory.CreateObject adapterModelFactory = FastObjectFactory.CreateObjectFactory <AdapterModel <TSource, TDestination> >(); Type destinationType = typeof(TDestination); Type sourceType = typeof(TSource); var fields = new List <FieldModel>(); var properties = new List <PropertyModel <TSource, TDestination> >(); MemberInfo[] destinationMembers = ReflectionUtils.GetPublicFieldsAndProperties(destinationType); int length = destinationMembers.Length; var config = TypeAdapterConfig <TSource, TDestination> .Configuration; bool hasConfig = config != null; for (int i = 0; i < length; i++) { MemberInfo destinationMember = destinationMembers[i]; bool isProperty = destinationMember is PropertyInfo; if (hasConfig) { #region Ignore Members var ignoreMembers = config.IgnoreMembers; if (ignoreMembers != null && ignoreMembers.Count > 0) { bool ignored = false; for (int j = 0; j < ignoreMembers.Count; j++) { if (destinationMember.Name.Equals(ignoreMembers[j])) { ignored = true; break; } } if (ignored) { continue; } } #endregion #region Custom Resolve var resolvers = config.Resolvers; if (resolvers != null && resolvers.Count > 0) { bool hasCustomResolve = false; for (int j = 0; j < resolvers.Count; j++) { var resolver = resolvers[j]; if (destinationMember.Name.Equals(resolver.MemberName)) { PropertyInfo destinationProperty = (PropertyInfo)destinationMember; var setter = PropertyCaller <TDestination> .CreateSetMethod(destinationProperty); if (setter == null) { continue; } var propertyModel = (PropertyModel <TSource, TDestination>)propertyModelFactory(); propertyModel.ConvertType = 5; propertyModel.Setter = setter; propertyModel.CustomResolver = resolver.Invoker; properties.Add(propertyModel); hasCustomResolve = true; break; } } if (hasCustomResolve) { continue; } } #endregion } MemberInfo sourceMember = ReflectionUtils.GetPublicFieldOrProperty(sourceType, isProperty, destinationMember.Name); if (sourceMember == null) { #region Flattening #region GetMethod var getMethod = sourceType.GetMethod(string.Concat("Get", destinationMember.Name)); if (getMethod != null) { var setter = PropertyCaller <TDestination> .CreateSetMethod((PropertyInfo)destinationMember); if (setter == null) { continue; } var propertyModel = (PropertyModel <TSource, TDestination>)propertyModelFactory(); propertyModel.ConvertType = 2; propertyModel.Setter = setter; propertyModel.AdaptInvoker = FastInvoker.GetMethodInvoker(getMethod); properties.Add(propertyModel); continue; } #endregion #region Class var delegates = new List <GenericGetter>(); GetDeepFlattening(sourceType, destinationMember.Name, delegates); if (delegates != null && delegates.Count > 0) { var setter = PropertyCaller <TDestination> .CreateSetMethod((PropertyInfo)destinationMember); if (setter == null) { continue; } var propertyModel = (PropertyModel <TSource, TDestination>)propertyModelFactory(); propertyModel.ConvertType = 3; propertyModel.Setter = setter; propertyModel.FlatteningInvokers = delegates.ToArray(); properties.Add(propertyModel); continue; } #endregion #endregion continue; } if (isProperty) { PropertyInfo destinationProperty = (PropertyInfo)destinationMember; var setter = PropertyCaller <TDestination> .CreateSetMethod(destinationProperty); if (setter == null) { continue; } PropertyInfo sourceProperty = (PropertyInfo)sourceMember; var getter = PropertyCaller <TSource> .CreateGetMethod(sourceProperty); if (getter == null) { continue; } Type destinationPropertyType = destinationProperty.PropertyType; var propertyModel = (PropertyModel <TSource, TDestination>)propertyModelFactory(); propertyModel.Getter = getter; propertyModel.Setter = setter; if (!ReflectionUtils.IsNullable(destinationPropertyType) && destinationPropertyType != typeof(string) && ReflectionUtils.IsPrimitive(destinationPropertyType)) { propertyModel.DefaultDestinationValue = Activator.CreateInstance(destinationPropertyType); } if (ReflectionUtils.IsPrimitive(destinationPropertyType)) { propertyModel.ConvertType = 1; var converter = ReflectionUtils.CreatePrimitiveConverter(sourceProperty.PropertyType, destinationPropertyType); if (converter != null) { propertyModel.AdaptInvoker = converter; } } else { propertyModel.ConvertType = 4; if (ReflectionUtils.IsCollection(destinationPropertyType)) //collections { propertyModel.AdaptInvoker = FastInvoker.GetMethodInvoker(typeof(CollectionAdapter <, ,>).MakeGenericType(sourceProperty.PropertyType, ReflectionUtils.ExtractElementType(destinationPropertyType), destinationPropertyType).GetMethod("Adapt", new Type[] { sourceProperty.PropertyType, typeof(Dictionary <,>).MakeGenericType(typeof(int), typeof(int)) })); } else // class { if (destinationPropertyType == sourceProperty.PropertyType) { bool newInstance; if (hasConfig && config.NewInstanceForSameType.HasValue) { newInstance = config.NewInstanceForSameType.Value; } else { newInstance = TypeAdapterConfig.Configuration.NewInstanceForSameType; } if (!newInstance) { propertyModel.ConvertType = 1; } else { propertyModel.AdaptInvoker = FastInvoker.GetMethodInvoker(typeof(ClassAdapter <,>).MakeGenericType(sourceProperty.PropertyType, destinationPropertyType).GetMethod("Adapt", new Type[] { sourceProperty.PropertyType, typeof(Dictionary <,>).MakeGenericType(typeof(int), typeof(int)) })); } } else { propertyModel.AdaptInvoker = FastInvoker.GetMethodInvoker(typeof(ClassAdapter <,>).MakeGenericType(sourceProperty.PropertyType, destinationPropertyType).GetMethod("Adapt", new Type[] { sourceProperty.PropertyType, typeof(Dictionary <,>).MakeGenericType(typeof(int), typeof(int)) })); } } } properties.Add(propertyModel); } else // Fields { FieldModel fieldModel = (FieldModel)fieldModelFactory(); var fieldInfoType = typeof(FieldInfo); fieldModel.Getter = FastInvoker.GetMethodInvoker(fieldInfoType.GetMethod("GetValue")); fieldModel.Setter = FastInvoker.GetMethodInvoker(fieldInfoType.GetMethod("SetValue")); fields.Add(fieldModel); } } var adapterModel = (AdapterModel <TSource, TDestination>)adapterModelFactory(); adapterModel.Fields = fields.ToArray(); adapterModel.Properties = properties.ToArray(); return(adapterModel); }