/// <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} 个对象映射关系"); }
/// <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 static void Create() { var attribute = new MapFromAttribute(typeof(MapFromAttributeTests)); Assert.Multiple(() => { Assert.That(attribute.Source, Is.EqualTo(typeof(MapFromAttributeTests))); Assert.That(attribute.ContainingNamespaceKind, Is.EqualTo(ContainingNamespaceKind.Source)); Assert.That(attribute.MatchingPropertyTypeKind, Is.EqualTo(MatchingPropertyTypeKind.Implicit)); }); }
void MapFromAttribureDoesSaveNamePassedToCtor() { var mapFromAttribure = new MapFromAttribute("Name passed to ctor"); Assert.Equal("Name passed to ctor", mapFromAttribure.Name); }
public static void Create() { var attribute = new MapFromAttribute(typeof(MapFromAttributeTests)); Assert.That(attribute.Source, Is.EqualTo(typeof(MapFromAttributeTests))); }