private bool MapDestinationPropertyToSource(IProfileConfiguration options, TypeDetails sourceTypeInfo, Type destType, string destMemberInfo, LinkedList <MemberInfo> members) { return (options.MemberConfigurations.Any( _ => _.MapDestinationPropertyToSource(options, sourceTypeInfo, destType, destMemberInfo, members))); }
//internal static ICollection<IChildMemberConfiguration> sourceToDestinationMemberMappers = new Collection<IChildMemberConfiguration> //{ // // Need to do it fixie way for prefix and postfix to work together + not specify match explicitly // // Have 3 properties for Members, Methods, And External Methods // // Parent goes to all // new MemberConfiguration().AddMember<NameSplitMember>().AddName<PrePostfixName>(_ => _.AddStrings(p => p.Prefixes, "Get")).SetMemberInfo<AllMemberInfo>(), // //new CustomizedSourceToDestinationMemberMapper().MemberNameMatch().ExtensionNameMatch().ExtensionPrefix("Get").MethodPrefix("Get").MethodNameMatch(), //}; //internal static readonly ICollection<IChildMemberConfiguration> def = sourceToDestinationMemberMappers.ToList(); public TypeMap CreateTypeMap(Type sourceType, Type destinationType, IProfileConfiguration options, MemberList memberList) { var sourceTypeInfo = GetTypeInfo(sourceType, options); var destTypeInfo = GetTypeInfo(destinationType, options); var typeMap = new TypeMap(sourceTypeInfo, destTypeInfo, memberList); foreach (var destProperty in destTypeInfo.PublicWriteAccessors) { var members = new LinkedList <MemberInfo>(); if (MapDestinationPropertyToSource(options, sourceTypeInfo, destProperty.GetType(), destProperty.Name, members)) { var resolvers = members.Select(mi => ReflectionHelper.ToMemberGetter(mi)); var destPropertyAccessor = ReflectionHelper.ToMemberAccessor(destProperty); typeMap.AddPropertyMap(destPropertyAccessor, resolvers.Cast <IValueResolver>()); } } if (!TypeExtensions.IsAbstract(destinationType) && TypeExtensions.IsClass(destinationType)) { foreach (var destCtor in destTypeInfo.Constructors.OrderByDescending(ci => ci.GetParameters().Length)) { if (MapDestinationCtorToSource(typeMap, destCtor, sourceTypeInfo, options)) { break; } } } return(typeMap); }
public bool MapDestinationPropertyToSource(IProfileConfiguration options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, LinkedList<IMemberGetter> resolvers, IMemberConfiguration parent ) { string[] matches = DestinationMemberNamingConvention.SplittingExpression .Matches(nameToSearch) .Cast<Match>() .Select(m => SourceMemberNamingConvention.ReplaceValue(m)) .ToArray(); MemberInfo matchingMemberInfo = null; for (int i = 1; i <= matches.Length; i++) { NameSnippet snippet = CreateNameSnippet(matches, i); matchingMemberInfo = parent.NameMapper.GetMatchingMemberInfo(sourceType, destType, destMemberType, snippet.First); if (matchingMemberInfo != null) { resolvers.AddLast(matchingMemberInfo.ToMemberGetter()); var details = new TypeDetails(matchingMemberInfo.GetMemberType(), options); var foundMatch = parent.MapDestinationPropertyToSource(options, details, destType, destMemberType, snippet.Second, resolvers); if (!foundMatch) resolvers.RemoveLast(); else break; } } return matchingMemberInfo != null; }
private bool MapDestinationCtorToSource(TypeMap typeMap, ConstructorInfo destCtor, TypeDetails sourceTypeInfo, IProfileConfiguration options) { var ctorParameters = destCtor.GetParameters(); if (ctorParameters.Length == 0 || !options.ConstructorMappingEnabled) return false; var ctorMap = new ConstructorMap(destCtor, typeMap); foreach (var parameter in ctorParameters) { var resolvers = new LinkedList<MemberInfo>(); var canResolve = MapDestinationPropertyToSource(options, sourceTypeInfo, destCtor.DeclaringType, parameter.GetType(), parameter.Name, resolvers); if(!canResolve && parameter.HasDefaultValue) { canResolve = true; } ctorMap.AddParameter(parameter, resolvers.ToArray(), canResolve); } typeMap.ConstructorMap = ctorMap; return true; }
public TypeMap CreateTypeMap(Type sourceType, Type destinationType, IProfileConfiguration options, MemberList memberList) { var sourceTypeInfo = new TypeDetails(sourceType, options); var destTypeInfo = new TypeDetails(destinationType, options); var typeMap = new TypeMap(sourceTypeInfo, destTypeInfo, memberList, options); foreach (var destProperty in destTypeInfo.PublicWriteAccessors) { var resolvers = new LinkedList <IMemberGetter>(); if (MapDestinationPropertyToSource(options, sourceTypeInfo, destProperty.DeclaringType, destProperty.GetMemberType(), destProperty.Name, resolvers)) { var destPropertyAccessor = destProperty.ToMemberAccessor(); typeMap.AddPropertyMap(destPropertyAccessor, resolvers); } } if (!destinationType.IsAbstract() && destinationType.IsClass()) { foreach (var destCtor in destTypeInfo.Constructors.OrderByDescending(ci => ci.GetParameters().Length)) { if (MapDestinationCtorToSource(typeMap, destCtor, sourceTypeInfo, options)) { break; } } } return(typeMap); }
private bool MapDestinationCtorToSource(TypeMap typeMap, ConstructorInfo destCtor, TypeDetails sourceTypeInfo, IProfileConfiguration options) { var parameters = new List <ConstructorParameterMap>(); var ctorParameters = destCtor.GetParameters(); if (ctorParameters.Length == 0 || !options.ConstructorMappingEnabled) { return(false); } foreach (var parameter in ctorParameters) { var members = new LinkedList <MemberInfo>(); var canResolve = MapDestinationPropertyToSource(options, sourceTypeInfo, parameter.GetType(), parameter.Name, members); var resolvers = members.Select(mi => ReflectionHelper.ToMemberGetter(mi)); var param = new ConstructorParameterMap(parameter, resolvers.ToArray(), canResolve); parameters.Add(param); } typeMap.AddConstructorMap(destCtor, parameters); return(true); }
public TypeMap CreateTypeMap(Type sourceType, Type destinationType, IProfileConfiguration options, MemberList memberList) { var sourceTypeInfo = options.CreateTypeDetails(sourceType); var destTypeInfo = options.CreateTypeDetails(destinationType); var typeMap = new TypeMap(sourceTypeInfo, destTypeInfo, memberList, options); foreach (var destProperty in destTypeInfo.PublicWriteAccessors) { var resolvers = new LinkedList<MemberInfo>(); if (MapDestinationPropertyToSource(options, sourceTypeInfo, destProperty.DeclaringType, destProperty.GetMemberType(), destProperty.Name, resolvers)) { typeMap.AddPropertyMap(destProperty, resolvers); } } if (!destinationType.IsAbstract() && destinationType.IsClass()) { foreach (var destCtor in destTypeInfo.Constructors.OrderByDescending(ci => ci.GetParameters().Length)) { if (MapDestinationCtorToSource(typeMap, destCtor, sourceTypeInfo, options)) { break; } } } return typeMap; }
private bool MapDestinationCtorToSource(TypeMap typeMap, ConstructorInfo destCtor, TypeDetails sourceTypeInfo, IProfileConfiguration options) { var parameters = new List<ConstructorParameterMap>(); var ctorParameters = destCtor.GetParameters(); if (ctorParameters.Length == 0 || !options.ConstructorMappingEnabled) return false; foreach (var parameter in ctorParameters) { var members = new LinkedList<MemberInfo>(); var canResolve = MapDestinationPropertyToSource(options, sourceTypeInfo, parameter.GetType(), parameter.Name, members); var resolvers = members.Select(mi => mi.ToMemberGetter()); var param = new ConstructorParameterMap(parameter, resolvers.ToArray(), canResolve); parameters.Add(param); } typeMap.AddConstructorMap(destCtor, parameters); return true; }
public bool MapDestinationPropertyToSource(IProfileConfiguration options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, LinkedList <MemberInfo> resolvers, IMemberConfiguration parent) { string[] matches = DestinationMemberNamingConvention.SplittingExpression .Matches(nameToSearch) .Cast <Match>() .Select(m => SourceMemberNamingConvention.ReplaceValue(m)) .ToArray(); MemberInfo matchingMemberInfo = null; for (int i = 1; i <= matches.Length; i++) { NameSnippet snippet = CreateNameSnippet(matches, i); matchingMemberInfo = parent.NameMapper.GetMatchingMemberInfo(sourceType, destType, destMemberType, snippet.First); if (matchingMemberInfo != null) { resolvers.AddLast(matchingMemberInfo); var details = options.CreateTypeDetails(matchingMemberInfo.GetMemberType()); var foundMatch = parent.MapDestinationPropertyToSource(options, details, destType, destMemberType, snippet.Second, resolvers); if (!foundMatch) { resolvers.RemoveLast(); } else { break; } } } return(matchingMemberInfo != null); }
//internal static ICollection<IChildMemberConfiguration> sourceToDestinationMemberMappers = new Collection<IChildMemberConfiguration> //{ // // Need to do it fixie way for prefix and postfix to work together + not specify match explicitly // // Have 3 properties for Members, Methods, And External Methods // // Parent goes to all // new MemberConfiguration().AddMember<NameSplitMember>().AddName<PrePostfixName>(_ => _.AddStrings(p => p.Prefixes, "Get")).SetMemberInfo<AllMemberInfo>(), // //new CustomizedSourceToDestinationMemberMapper().MemberNameMatch().ExtensionNameMatch().ExtensionPrefix("Get").MethodPrefix("Get").MethodNameMatch(), //}; //internal static readonly ICollection<IChildMemberConfiguration> def = sourceToDestinationMemberMappers.ToList(); public TypeMap CreateTypeMap(Type sourceType, Type destinationType, IProfileConfiguration options, MemberList memberList) { var sourceTypeInfo = GetTypeInfo(sourceType, options); var destTypeInfo = GetTypeInfo(destinationType, options); var typeMap = new TypeMap(sourceTypeInfo, destTypeInfo, memberList); foreach (var destProperty in destTypeInfo.PublicWriteAccessors) { var members = new LinkedList<MemberInfo>(); if (MapDestinationPropertyToSource(options, sourceTypeInfo, destProperty.GetType(), destProperty.Name, members)) { var resolvers = members.Select(mi => ReflectionHelper.ToMemberGetter(mi)); var destPropertyAccessor = ReflectionHelper.ToMemberAccessor(destProperty); typeMap.AddPropertyMap(destPropertyAccessor, resolvers.Cast<IValueResolver>()); } } if (!TypeExtensions.IsAbstract(destinationType) && TypeExtensions.IsClass(destinationType)) { foreach (var destCtor in destTypeInfo.Constructors.OrderByDescending(ci => ci.GetParameters().Length)) { if (MapDestinationCtorToSource(typeMap, destCtor, sourceTypeInfo, options)) { break; } } } return typeMap; }
public ProfileMap(IProfileConfiguration profile, IConfiguration configuration) { _typeDetails = new LockingConcurrentDictionary <Type, TypeDetails>(TypeDetailsFactory); _extraMembersByType = profile.ExtraMembersByType; _extraMemberGetterStrategies = profile.ExtraMemberGetterStrategies; _extraMemberSetterStrategies = profile.ExtraMemberSetterStrategies; Name = profile.ProfileName; AllowNullCollections = profile.AllowNullCollections ?? configuration?.AllowNullCollections ?? false; AllowNullDestinationValues = profile.AllowNullDestinationValues ?? configuration?.AllowNullDestinationValues ?? true; EnableNullPropagationForQueryMapping = profile.EnableNullPropagationForQueryMapping ?? configuration?.EnableNullPropagationForQueryMapping ?? false; ConstructorMappingEnabled = profile.ConstructorMappingEnabled ?? configuration?.ConstructorMappingEnabled ?? true; ShouldMapField = profile.ShouldMapField ?? configuration?.ShouldMapField ?? (p => p.IsPublic()); ShouldMapProperty = profile.ShouldMapProperty ?? configuration?.ShouldMapProperty ?? (p => p.IsPublic()); CreateMissingTypeMaps = profile.CreateMissingTypeMaps ?? configuration?.CreateMissingTypeMaps ?? false; TypeConfigurations = profile.TypeConfigurations .Concat(configuration?.TypeConfigurations ?? Enumerable.Empty <IConditionalObjectMapper>()) .Concat(CreateMissingTypeMaps ? Enumerable.Repeat(new ConditionalObjectMapper { Conventions = { tp => !ExcludedTypes.Contains(tp.SourceType) && !ExcludedTypes.Contains(tp.DestinationType) } }, 1) : Enumerable.Empty <IConditionalObjectMapper>()) .ToArray(); ValueTransformers = profile.ValueTransformers.Concat(configuration?.ValueTransformers ?? Enumerable.Empty <ValueTransformerConfiguration>()).ToArray(); MemberConfigurations = profile.MemberConfigurations.ToArray(); MemberConfigurations.FirstOrDefault()?.AddMember <NameSplitMember>(_ => _.SourceMemberNamingConvention = profile.SourceMemberNamingConvention); MemberConfigurations.FirstOrDefault()?.AddMember <NameSplitMember>(_ => _.DestinationMemberNamingConvention = profile.DestinationMemberNamingConvention); GlobalIgnores = profile.GlobalIgnores.Concat(configuration?.GlobalIgnores ?? Enumerable.Empty <string>()).ToArray(); SourceExtensionMethods = profile.SourceExtensionMethods.Concat(configuration?.SourceExtensionMethods ?? Enumerable.Empty <MethodInfo>()).ToArray(); AllPropertyMapActions = profile.AllPropertyMapActions.Concat(configuration?.AllPropertyMapActions ?? Enumerable.Empty <Action <PropertyMap, IMemberConfigurationExpression> >()).ToArray(); AllTypeMapActions = profile.AllTypeMapActions.Concat(configuration?.AllTypeMapActions ?? Enumerable.Empty <Action <TypeMap, IMappingExpression> >()).ToArray(); Prefixes = profile.MemberConfigurations .Select(m => m.NameMapper) .SelectMany(m => m.NamedMappers) .OfType <PrePostfixName>() .SelectMany(m => m.Prefixes) .ToArray(); Postfixes = profile.MemberConfigurations .Select(m => m.NameMapper) .SelectMany(m => m.NamedMappers) .OfType <PrePostfixName>() .SelectMany(m => m.Postfixes) .ToArray(); _typeMapConfigs = profile.TypeMapConfigs.ToArray(); _openTypeMapConfigs = profile.OpenTypeMapConfigs.ToArray(); }
public TypeMap(TypeDetails sourceType, TypeDetails destinationType, MemberList memberList, IProfileConfiguration profile) { SourceTypeDetails = sourceType; DestinationTypeDetails = destinationType; Types = new TypePair(sourceType.Type, destinationType.Type); Profile = profile; ConfiguredMemberList = memberList; IgnorePropertiesStartingWith = profile.GlobalIgnores; }
public static TypeDetails GetTypeInfo(Type type, IProfileConfiguration profileConfiguration) { var typeInfo = _typeInfos.GetOrAdd(type, t => new TypeDetails(type, profileConfiguration.ShouldMapProperty, profileConfiguration.ShouldMapField, profileConfiguration.SourceExtensionMethods)); return typeInfo; }
public ProfileManager(IProfileConfiguration config) { profilerType = config.Profiler; if (!typeof (IProfiler).IsAssignableFrom(profilerType)) { throw new InitialisationException(profilerType.FullName + " is not an IProfiler"); } forFacetTypes = config.EventsToProfile.Select(e => EventToFacetMap[e]).ToArray(); }
public TypeMap(TypeDetails sourceType, TypeDetails destinationType, MemberList memberList, IProfileConfiguration profile) { SourceTypeDetails = sourceType; DestinationTypeDetails = destinationType; Types = new TypePair(sourceType.Type, destinationType.Type); Profile = profile; ConfiguredMemberList = memberList; IgnorePropertiesStartingWith = profile.GlobalIgnores; }
public static TypeDetails GetTypeInfo(Type type, IProfileConfiguration profileConfiguration) { var typeInfo = _typeInfos.GetOrAdd(type, t => new TypeDetails(type, profileConfiguration.ShouldMapProperty, profileConfiguration.ShouldMapField, profileConfiguration.SourceExtensionMethods)); return(typeInfo); }
public DragonflySpeechBackend(IRules rules, ILog log, IProfileConfiguration profileConfiguration, IPaths paths) { this.rules = rules; this.log = log; this.profileConfiguration = profileConfiguration; this.paths = paths; GrammarDirectory = paths.GetPath("Dragonfly"); ControllerDirectory = Path.Combine(GrammarDirectory, "Controllers"); }
public bool MapDestinationPropertyToSource(IProfileConfiguration options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, LinkedList<IMemberGetter> resolvers, IMemberConfiguration parent = null) { if (string.IsNullOrEmpty(nameToSearch)) return true; var matchingMemberInfo = NameMapper.GetMatchingMemberInfo(sourceType, destType, destMemberType, nameToSearch); if (matchingMemberInfo != null) resolvers.AddLast(matchingMemberInfo.ToMemberGetter()); return matchingMemberInfo != null; }
/// <summary>Method for configuring mappers.</summary> /// <param name="configuration">The configuration.</param> public void Configure(IProfileConfiguration configuration) { configuration.Configure <Artist>("artist2", x => x.HasKey(y => y.ArtistId), true); configuration.CreateMap <DomainArtist, Artist>("artist").ConvertUsing <ArtistConverter>(); configuration.Configure <DomainTrack>("track").ConfigureUsing <TrackConfigurator>(); // TODO: ITypeConfigurator must be able to map stuff configuration.Configure <DomainTrack>("track").ForEntity(x => x.HasKey(y => y.Wbs)).CreateMap <Track>().ConvertUsing <TrackConverter>(); configuration.Configure <Track>("Track", x => x.HasKey(y => y.TrackId)); }
public ProfileManager(IProfileConfiguration config) { profilerType = config.Profiler; if (!typeof(IProfiler).IsAssignableFrom(profilerType)) { throw new InitialisationException(profilerType.FullName + " is not an IProfiler"); } forFacetTypes = config.EventsToProfile.Select(e => EventToFacetMap[e]).ToArray(); }
public TypeDetails(Type type, IProfileConfiguration config) { Type = type; var membersToMap = MembersToMap(config.ShouldMapProperty, config.ShouldMapField); var publicReadableMembers = GetAllPublicReadableMembers(membersToMap); var publicWritableMembers = GetAllPublicWritableMembers(membersToMap); PublicReadAccessors = BuildPublicReadAccessors(publicReadableMembers); PublicWriteAccessors = BuildPublicAccessors(publicWritableMembers); PublicNoArgMethods = BuildPublicNoArgMethods(); Constructors = type.GetDeclaredConstructors().Where(ci => !ci.IsStatic).ToArray(); PublicNoArgExtensionMethods = BuildPublicNoArgExtensionMethods(config.SourceExtensionMethods); }
/// <summary> /// 构造函数 /// </summary> /// <param name="profile">配置</param> public MapToExpression(IProfileConfiguration profile) { if (profile is null) { throw new ArgumentNullException(nameof(profile)); } ServiceCtor = profile.ServiceCtor ?? Activator.CreateInstance; Kind = profile.Kind; AllowNullDestinationValues = profile.AllowNullDestinationValues ?? true; AllowNullPropagationMapping = profile.AllowNullPropagationMapping ?? false; }
public bool MapDestinationPropertyToSource(IProfileConfiguration options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, LinkedList <IMemberGetter> resolvers) { var foundMap = false; foreach (var memberMapper in MemberMappers) { foundMap = memberMapper.MapDestinationPropertyToSource(options, sourceType, destType, destMemberType, nameToSearch, resolvers, this); if (foundMap) { break; } } return(foundMap); }
public ProfileMap(IProfileConfiguration profile, IConfiguration configuration) { _typeDetails = new LockingConcurrentDictionary <Type, TypeDetails>(TypeDetailsFactory); Name = profile.ProfileName; AllowNullCollections = profile.AllowNullCollections ?? configuration?.AllowNullCollections ?? false; AllowNullDestinationValues = profile.AllowNullDestinationValues ?? configuration?.AllowNullDestinationValues ?? true; EnableNullPropagationForQueryMapping = profile.EnableNullPropagationForQueryMapping ?? configuration?.EnableNullPropagationForQueryMapping ?? false; ConstructorMappingEnabled = profile.ConstructorMappingEnabled ?? configuration?.ConstructorMappingEnabled ?? true; ShouldMapField = profile.ShouldMapField ?? configuration?.ShouldMapField ?? (p => p.IsPublic()); ShouldMapProperty = profile.ShouldMapProperty ?? configuration?.ShouldMapProperty ?? (p => p.IsPublic()); ShouldMapMethod = profile.ShouldMapMethod ?? configuration?.ShouldMapMethod ?? (p => true); ShouldUseConstructor = profile.ShouldUseConstructor ?? configuration?.ShouldUseConstructor ?? (c => true); ValueTransformers = profile.ValueTransformers.Concat(configuration?.ValueTransformers ?? Enumerable.Empty <ValueTransformerConfiguration>()).ToArray(); _memberConfigurations = profile.MemberConfigurations.Concat(configuration?.MemberConfigurations ?? Enumerable.Empty <IMemberConfiguration>()).ToArray(); var nameSplitMember = _memberConfigurations[0].MemberMappers.OfType <NameSplitMember>().FirstOrDefault(); if (nameSplitMember != null) { nameSplitMember.SourceMemberNamingConvention = profile.SourceMemberNamingConvention; nameSplitMember.DestinationMemberNamingConvention = profile.DestinationMemberNamingConvention; } GlobalIgnores = profile.GlobalIgnores.Concat(configuration?.GlobalIgnores ?? Enumerable.Empty <string>()).ToArray(); SourceExtensionMethods = profile.SourceExtensionMethods.Concat(configuration?.SourceExtensionMethods ?? Enumerable.Empty <MethodInfo>()).ToArray(); AllPropertyMapActions = profile.AllPropertyMapActions.Concat(configuration?.AllPropertyMapActions ?? Enumerable.Empty <Action <PropertyMap, IMemberConfigurationExpression> >()).ToArray(); AllTypeMapActions = profile.AllTypeMapActions.Concat(configuration?.AllTypeMapActions ?? Enumerable.Empty <Action <TypeMap, IMappingExpression> >()).ToArray(); Prefixes = profile.MemberConfigurations .Select(m => m.NameMapper) .SelectMany(m => m.NamedMappers) .OfType <PrePostfixName>() .SelectMany(m => m.Prefixes) .ToList(); Postfixes = profile.MemberConfigurations .Select(m => m.NameMapper) .SelectMany(m => m.NamedMappers) .OfType <PrePostfixName>() .SelectMany(m => m.Postfixes) .ToList(); _typeMapConfigs = profile.TypeMapConfigs.ToArray(); _openTypeMapConfigs = profile.OpenTypeMapConfigs.ToArray(); }
public ProfileMap(IProfileConfiguration profile, IConfiguration configuration) { _typeDetails = new LockingConcurrentDictionary<Type, TypeDetails>(TypeDetailsFactory); Name = profile.ProfileName; AllowNullCollections = profile.AllowNullCollections ?? configuration?.AllowNullCollections ?? false; AllowNullDestinationValues = profile.AllowNullDestinationValues ?? configuration?.AllowNullDestinationValues ?? true; EnableNullPropagationForQueryMapping = profile.EnableNullPropagationForQueryMapping ?? configuration?.EnableNullPropagationForQueryMapping ?? false; ConstructorMappingEnabled = profile.ConstructorMappingEnabled ?? configuration?.ConstructorMappingEnabled ?? true; ShouldMapField = profile.ShouldMapField ?? configuration?.ShouldMapField ?? (p => p.IsPublic()); ShouldMapProperty = profile.ShouldMapProperty ?? configuration?.ShouldMapProperty ?? (p => p.IsPublic()); CreateMissingTypeMaps = profile.CreateMissingTypeMaps ?? configuration?.CreateMissingTypeMaps ?? false; TypeConfigurations = profile.TypeConfigurations .Concat(configuration?.TypeConfigurations ?? Enumerable.Empty<IConditionalObjectMapper>()) .Concat(CreateMissingTypeMaps ? Enumerable.Repeat(new ConditionalObjectMapper { Conventions = { tp => tp.SourceType != typeof(object) && tp.DestinationType != typeof(object) } }, 1) : Enumerable.Empty<IConditionalObjectMapper>()) .ToArray(); MemberConfigurations = profile.MemberConfigurations.ToArray(); MemberConfigurations.FirstOrDefault()?.AddMember<NameSplitMember>(_ => _.SourceMemberNamingConvention = profile.SourceMemberNamingConvention); MemberConfigurations.FirstOrDefault()?.AddMember<NameSplitMember>(_ => _.DestinationMemberNamingConvention = profile.DestinationMemberNamingConvention); GlobalIgnores = profile.GlobalIgnores.Concat(configuration?.GlobalIgnores ?? Enumerable.Empty<string>()).ToArray(); SourceExtensionMethods = profile.SourceExtensionMethods.Concat(configuration?.SourceExtensionMethods ?? Enumerable.Empty<MethodInfo>()).ToArray(); AllPropertyMapActions = profile.AllPropertyMapActions.Concat(configuration?.AllPropertyMapActions ?? Enumerable.Empty<Action<PropertyMap, IMemberConfigurationExpression>>()).ToArray(); AllTypeMapActions = profile.AllTypeMapActions.Concat(configuration?.AllTypeMapActions ?? Enumerable.Empty<Action<TypeMap, IMappingExpression>>()).ToArray(); Prefixes = profile.MemberConfigurations .Select(m => m.NameMapper) .SelectMany(m => m.NamedMappers) .OfType<PrePostfixName>() .SelectMany(m => m.Prefixes) .ToArray(); Postfixes = profile.MemberConfigurations .Select(m => m.NameMapper) .SelectMany(m => m.NamedMappers) .OfType<PrePostfixName>() .SelectMany(m => m.Postfixes) .ToArray(); _typeMapConfigs = profile.TypeMapConfigs.ToArray(); _openTypeMapConfigs = profile.OpenTypeMapConfigs.ToArray(); }
public bool MapDestinationPropertyToSource(IProfileConfiguration options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, LinkedList <IMemberGetter> resolvers, IMemberConfiguration parent = null) { if (string.IsNullOrEmpty(nameToSearch)) { return(true); } var matchingMemberInfo = NameMapper.GetMatchingMemberInfo(sourceType, destType, destMemberType, nameToSearch); if (matchingMemberInfo != null) { resolvers.AddLast(matchingMemberInfo.ToMemberGetter()); } return(matchingMemberInfo != null); }
protected override void Establish_context() { var namingConvention = new StubNamingConvention(s => s.Value.ToLower()) { SeparatorCharacter = "__", SplittingExpression = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=__?)") }; _mappingOptions = new Profile("Test"); _mappingOptions.AddMemberConfiguration().AddMember <NameSplitMember>(_ => { _.SourceMemberNamingConvention = new PascalCaseNamingConvention(); _.DestinationMemberNamingConvention = namingConvention; }); _factory = new TypeMapFactory(); }
public TypeDetails(Type type, IProfileConfiguration config) { Type = type; var membersToMap = MembersToMap(config.ShouldMapProperty, config.ShouldMapField); var publicReadableMembers = GetAllPublicReadableMembers(membersToMap); var publicWritableMembers = GetAllPublicWritableMembers(membersToMap); PublicReadAccessors = BuildPublicReadAccessors(publicReadableMembers); PublicWriteAccessors = BuildPublicAccessors(publicWritableMembers); PublicNoArgMethods = BuildPublicNoArgMethods(); Constructors = type.GetDeclaredConstructors().Where(ci => !ci.IsStatic).ToArray(); PublicNoArgExtensionMethods = BuildPublicNoArgExtensionMethods(config.SourceExtensionMethods); AllMembers = PublicReadAccessors.Concat(PublicNoArgMethods).Concat(PublicNoArgExtensionMethods).ToList(); DestinationMemberNames = AllMembers.Select(mi => new DestinationMemberName { Member = mi, Possibles = PossibleNames(mi.Name, config.Prefixes, config.Postfixes).ToArray() }); }
public ProfileMap(IProfileConfiguration profile) : this(profile, null) { }
public ProfileMap(IProfileConfiguration profile, IGlobalConfigurationExpression configuration = null) { var globalProfile = (IProfileConfiguration)configuration; Name = profile.ProfileName; AllowNullCollections = profile.AllowNullCollections ?? configuration?.AllowNullCollections ?? false; AllowNullDestinationValues = profile.AllowNullDestinationValues ?? configuration?.AllowNullDestinationValues ?? true; EnableNullPropagationForQueryMapping = profile.EnableNullPropagationForQueryMapping ?? configuration?.EnableNullPropagationForQueryMapping ?? false; ConstructorMappingEnabled = profile.ConstructorMappingEnabled ?? globalProfile?.ConstructorMappingEnabled ?? true; MethodMappingEnabled = profile.MethodMappingEnabled ?? globalProfile?.MethodMappingEnabled ?? true; FieldMappingEnabled = profile.FieldMappingEnabled ?? globalProfile?.FieldMappingEnabled ?? true; ShouldMapField = profile.ShouldMapField ?? configuration?.ShouldMapField ?? (p => p.IsPublic); ShouldMapProperty = profile.ShouldMapProperty ?? configuration?.ShouldMapProperty ?? (p => p.IsPublic()); ShouldMapMethod = profile.ShouldMapMethod ?? configuration?.ShouldMapMethod ?? (p => !p.IsSpecialName); ShouldUseConstructor = profile.ShouldUseConstructor ?? configuration?.ShouldUseConstructor ?? (c => true); ValueTransformers = profile.ValueTransformers.Concat(configuration?.ValueTransformers).ToArray(); _memberConfigurations = profile.MemberConfigurations.Concat(globalProfile?.MemberConfigurations).ToArray(); var nameSplitMember = _memberConfigurations[0].MemberMappers.OfType <NameSplitMember>().FirstOrDefault(); if (nameSplitMember != null) { nameSplitMember.SourceMemberNamingConvention = profile.SourceMemberNamingConvention ?? PascalCaseNamingConvention.Instance; nameSplitMember.DestinationMemberNamingConvention = profile.DestinationMemberNamingConvention ?? PascalCaseNamingConvention.Instance; } GlobalIgnores = profile.GlobalIgnores.Concat(globalProfile?.GlobalIgnores).ToArray(); SourceExtensionMethods = profile.SourceExtensionMethods.Concat(globalProfile?.SourceExtensionMethods).ToArray(); AllPropertyMapActions = profile.AllPropertyMapActions.Concat(globalProfile?.AllPropertyMapActions).ToArray(); AllTypeMapActions = profile.AllTypeMapActions.Concat(globalProfile?.AllTypeMapActions).ToArray(); var prePostFixes = profile.MemberConfigurations.Concat(globalProfile?.MemberConfigurations) .Select(m => m.NameMapper) .SelectMany(m => m.NamedMappers) .OfType <PrePostfixName>() .ToArray(); Prefixes = prePostFixes.SelectMany(m => m.Prefixes).Distinct().ToList(); Postfixes = prePostFixes.SelectMany(m => m.Postfixes).Distinct().ToList(); SetTypeMapConfigs(); SetOpenTypeMapConfigs(); _typeDetails = new(2 * _typeMapConfigs.Length); return; void SetTypeMapConfigs() { _typeMapConfigs = new ITypeMapConfiguration[profile.TypeMapConfigs.Count]; var index = 0; var reverseMapsCount = 0; foreach (var typeMapConfig in profile.TypeMapConfigs) { _typeMapConfigs[index++] = typeMapConfig; if (typeMapConfig.ReverseTypeMap != null) { reverseMapsCount++; } } TypeMapsCount = index + reverseMapsCount; } void SetOpenTypeMapConfigs() { _openTypeMapConfigs = new(profile.OpenTypeMapConfigs.Count); foreach (var openTypeMapConfig in profile.OpenTypeMapConfigs) { _openTypeMapConfigs.Add(openTypeMapConfig.Types, openTypeMapConfig); var reverseMap = openTypeMapConfig.ReverseTypeMap; if (reverseMap != null) { _openTypeMapConfigs.Add(reverseMap.Types, reverseMap); } } } }
private bool MapDestinationCtorToSource(TypeMap typeMap, ConstructorInfo destCtor, TypeDetails sourceTypeInfo, IProfileConfiguration options) { var ctorParameters = destCtor.GetParameters(); if (ctorParameters.Length == 0 || !options.ConstructorMappingEnabled) { return(false); } var ctorMap = new ConstructorMap(destCtor, typeMap); foreach (var parameter in ctorParameters) { var resolvers = new LinkedList <IMemberGetter>(); var canResolve = MapDestinationPropertyToSource(options, sourceTypeInfo, destCtor.DeclaringType, parameter.GetType(), parameter.Name, resolvers); if (!canResolve && parameter.HasDefaultValue) { canResolve = true; } ctorMap.AddParameter(parameter, resolvers.ToArray(), canResolve); } typeMap.ConstructorMap = ctorMap; return(true); }
protected override void Establish_context() { var namingConvention = new StubNamingConvention(s => s.Value.ToLower()){SeparatorCharacter = "__", SplittingExpression = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=__?)")}; _mappingOptions = new Profile("Test"); _mappingOptions.AddMemberConfiguration().AddMember<NameSplitMember>(_ => { _.SourceMemberNamingConvention = namingConvention; _.DestinationMemberNamingConvention = new PascalCaseNamingConvention(); }); _factory = new TypeMapFactory(); }
public ProfileMap(IProfileConfiguration profile, IConfiguration configuration) { _typeDetails = new LockingConcurrentDictionary <Type, TypeDetails>(t => new TypeDetails(t, this)); _typeMapConfigs = profile.TypeMapConfigs.ToArray(); }
public TypeDetails(Type type, IProfileConfiguration config) : this(type, config.ShouldMapProperty, config.ShouldMapField, config.SourceExtensionMethods) { }
private bool MapDestinationPropertyToSource(IProfileConfiguration options, TypeDetails sourceTypeInfo, Type destType, string destMemberInfo, LinkedList<MemberInfo> members) { return options.MemberConfigurations.Any( _ => _.MapDestinationPropertyToSource(options, sourceTypeInfo, destType, destMemberInfo, members)); }
public TypeDetails(Type type, IProfileConfiguration config) : this(type, config.ShouldMapProperty, config.ShouldMapField, config.SourceExtensionMethods) { }
/// <summary> /// 构造函数。 /// </summary> /// <param name="profile">配置。</param> public CopyToExpression(IProfileConfiguration profile) : base(profile) { }
public ProfileMap(IProfileConfiguration profile) : this(profile, null) { }