/// <summary> /// 执行对象映射构造 /// </summary> public void CreateMap() { List <(Type Source, Type Target)> tuples = new List <(Type Source, Type Target)>(); Type[] types = AssemblyManager.FindTypesByAttribute <MapFromAttribute>(); foreach (Type targetType in types) { MapFromAttribute attribute = targetType.GetAttribute <MapFromAttribute>(true); foreach (Type sourceType in attribute.SourceTypes) { var tuple = ValueTuple.Create(sourceType, targetType); tuples.AddIfNotExist(tuple); } } types = AssemblyManager.FindTypesByAttribute <MapToAttribute>(); foreach (Type sourceType in types) { MapToAttribute attribute = sourceType.GetAttribute <MapToAttribute>(true); foreach (Type targetType in attribute.TargetTypes) { var tuple = ValueTuple.Create(sourceType, targetType); tuples.AddIfNotExist(tuple); } } foreach ((Type Source, Type Target)tuple in tuples) { CreateMap(tuple.Source, tuple.Target); _logger.LogDebug($"创建“{tuple.Source}”到“{tuple.Target}”的对象映射关系"); } _logger.LogInformation($"创建了 {tuples.Count} 个对象映射关系"); }
public void TestTypeOnlyConstructor() { Type mappedType = typeof(string); MapToAttribute testAttribute = new MapToAttribute(mappedType); Assert.AreEqual(mappedType, testAttribute.MappedType); Assert.AreEqual(String.Empty, testAttribute.Identifier); }
/// <inheritdoc /> public void CreateMap() { List <(Type Source, Type Target)> tuples = new List <(Type Source, Type Target)>(); Type[] types = _mapFromAttributeTypeFinder.FindAll(true); foreach (Type targetType in types) { MapFromAttribute attribute = targetType.GetAttribute <MapFromAttribute>(); foreach (Type sourceType in attribute.SourceTypes) { var tuple = ValueTuple.Create(sourceType, targetType); tuples.AddIfNotExist(tuple); } } types = _mapToAttributeTypeFinder.FindAll(true); foreach (Type sourceType in types) { MapToAttribute attribute = sourceType.GetAttribute <MapToAttribute>(); foreach (Type targetType in attribute.TargetTypes) { var tuple = ValueTuple.Create(sourceType, targetType); tuples.AddIfNotExist(tuple); } } foreach ((Type Source, Type Target)tuple in tuples) { CreateMap(tuple.Source, tuple.Target); } }
public void TestTypeStringConstructor() { Type mappedType = typeof(string); string identifier = "identifier"; MapToAttribute testAttribute = new MapToAttribute(mappedType, identifier); Assert.AreEqual(mappedType, testAttribute.MappedType); Assert.AreEqual(identifier, testAttribute.Identifier); }
public static void Create() { var attribute = new MapToAttribute(typeof(MapToAttributeTests)); Assert.Multiple(() => { Assert.That(attribute.Destination, Is.EqualTo(typeof(MapToAttributeTests))); Assert.That(attribute.ContainingNamespaceKind, Is.EqualTo(ContainingNamespaceKind.Source)); Assert.That(attribute.MatchingPropertyTypeKind, Is.EqualTo(MatchingPropertyTypeKind.Implicit)); }); }
//public static IQueryable<T> SelectFields<T>(this IQueryable<T> query, string fields = "") //{ // string[] EntityFields; // if (fields == "") // // get Properties of the T // EntityFields = typeof(T).GetProperties().Select(propertyInfo => propertyInfo.Name).ToArray(); // else // EntityFields = fields.Split(','); // // input parameter "o" // var xParameter = Expression.Parameter(typeof(T), "o"); // // new statement "new Data()" // var xNew = Expression.New(typeof(T)); // // create initializers // var bindings = EntityFields.Select(o => o.Trim()) // .Select(o => // { // // property "Field1" // var mi = typeof(T).GetProperty(o); // // original value "o.Field1" // var xOriginal = Expression.Property(xParameter, mi); // // set value "Field1 = o.Field1" // return Expression.Bind(mi, xOriginal); // } // ); // // initialization "new Data { Field1 = o.Field1, Field2 = o.Field2 }" // var xInit = Expression.MemberInit(xNew, bindings); // // expression "o => new Data { Field1 = o.Field1, Field2 = o.Field2 }" // var lambda = Expression.Lambda<Func<T, T>>(xInit, xParameter); // // compile to Func<Data, Data> // return query.Select(lambda.Compile()).AsQueryable(); //} //"Id,Name,CountryCode,Cities(Id,Name,Areas(Id,Name))" private static string GetMapToName(string propName, Type parentType) { string mapToName = string.Empty; var property = parentType.GetProperty(propName); var attrs = property.GetCustomAttributes(true); foreach (object attr in attrs) { MapToAttribute mapToAttr = attr as MapToAttribute; if (mapToAttr != null) { mapToName = mapToAttr.Name; } } return(mapToName); }
public static IMappingExpression <TSource, TDestination> MapTo <TSource, TDestination>(this IMappingExpression <TSource, TDestination> expression) { Type sourceType = typeof(TSource); Type destinationType = typeof(TDestination); TypeMap existingMaps = Mapper.GetAllTypeMaps().First(b => b.SourceType == sourceType && b.DestinationType == destinationType); string[] missingMappings = existingMaps.GetUnmappedPropertyNames(); if (missingMappings.Any()) { PropertyInfo[] sourceProperties = sourceType.GetProperties(); foreach (string property in missingMappings) { foreach (PropertyInfo propertyInfo in sourceProperties) { MapToAttribute attr = propertyInfo.GetCustomAttribute <MapToAttribute>(); if (attr != null && attr.Name == property) { expression.ForMember(property, opt => opt.ResolveUsing(new MyValueResolve(propertyInfo))); } } } } return(expression); }
public static void Create() { var attribute = new MapToAttribute(typeof(MapToAttributeTests)); Assert.That(attribute.Destination, Is.EqualTo(typeof(MapToAttributeTests))); }