├── .config
└── dotnet-tools.json
├── .github
└── workflows
│ └── dotnet-buildandtest.yml
├── .gitignore
├── LICENSE
├── README.md
└── src
├── .editorconfig
├── Benchmark
├── Benchmark.csproj
├── Benchmarks
│ ├── Config.cs
│ ├── TestAll.cs
│ ├── TestComplexTypes.cs
│ └── TestSimpleTypes.cs
├── Classes
│ ├── Customer.cs
│ └── Foo.cs
├── CustomerMapper.g.cs
├── CustomerMapper.tt
├── FooMapper.g.cs
├── FooMapper.tt
├── Program.cs
└── TestAdaptHelper.cs
├── Directory.Build.props
├── ExpressionDebugger.Console
├── ExpressionDebugger.Console.csproj
└── Program.cs
├── ExpressionDebugger.Tests
├── DebugInfoInjectorTest.cs
└── ExpressionDebugger.Tests.csproj
├── ExpressionDebugger.sln
├── ExpressionDebugger
├── ExpressionCompilationOptions.cs
├── ExpressionCompiler.cs
├── ExpressionDebugger.csproj
├── ExpressionDebugger.snk
└── ExpressionDebuggerExtensions.cs
├── ExpressionTranslator
├── ExpressionDefinitions.cs
├── ExpressionTranslator.cs
├── ExpressionTranslator.csproj
├── ExpressionTranslator.snk
├── ExpressionTranslatorExtensions.cs
├── Extensions.cs
├── PropertyDefinitions.cs
├── TypeDefinitions.cs
└── icon.png
├── Mapster.Async.Tests
├── AsyncTest.cs
└── Mapster.Async.Tests.csproj
├── Mapster.Async
├── Mapster.Async.csproj
├── Mapster.Async.snk
├── TypeAdapterExtensions.cs
└── icon.png
├── Mapster.Core
├── Attributes
│ ├── AdaptIgnoreAttribute.cs
│ ├── AdaptMemberAttribute.cs
│ ├── BaseAdaptAttribute.cs
│ ├── GenerateMapperAttribute.cs
│ ├── MapperAttribute.cs
│ ├── PropertyTypeAttribute.cs
│ └── UseDestinationValueAttribute.cs
├── Enums
│ ├── MapType.cs
│ └── MemberSide.cs
├── MapContext
│ ├── MapContext.cs
│ └── MapContextScope.cs
├── Mapster.Core.csproj
├── Mapster.Core.csproj.DotSettings
├── Mapster.Core.snk
├── Models
│ └── Never.cs
├── Register
│ ├── AdaptAttributeBuilder.cs
│ ├── CodeGenerationConfig.cs
│ ├── GenerateMapperAttributeBuilder.cs
│ ├── ICodeGenerationRegister.cs
│ ├── PropertySetting.cs
│ └── PropertySettingBuilder.cs
├── Utils
│ ├── Enum.cs
│ ├── Extensions.cs
│ ├── MapsterHelper.cs
│ ├── NameHelper.cs
│ └── ReferenceTuple.cs
└── icon.png
├── Mapster.DependencyInjection.Tests
├── InjectionTest.cs
└── Mapster.DependencyInjection.Tests.csproj
├── Mapster.DependencyInjection
├── Mapster.DependencyInjection.csproj
├── Mapster.DependencyInjection.snk
├── ServiceCollectionExtensions.cs
├── ServiceMapper.cs
├── TypeAdapterExtensions.cs
└── icon.png
├── Mapster.EF6
├── Mapster.EF6.csproj
├── Mapster.EF6.snk
├── TypeAdapterBuilderExtensions.cs
├── icon.png
└── packages.config
├── Mapster.EFCore.Tests
├── DbInitializer.cs
├── EFCoreTest.cs
├── Mapster.EFCore.Tests.csproj
└── Models
│ ├── Course.cs
│ ├── Enrollment.cs
│ ├── SchoolContext.cs
│ └── Student.cs
├── Mapster.EFCore
├── Mapster.EFCore.csproj
├── Mapster.EFCore.snk
├── MapsterQueryable.cs
├── TypeAdapterBuilderExtensions.cs
├── icon.png
└── packages.config
├── Mapster.Immutable.Tests
├── ImmutableTest.cs
└── Mapster.Immutable.Tests.csproj
├── Mapster.Immutable
├── ImmutableAdapter.cs
├── Mapster.Immutable.csproj
├── Mapster.Immutable.snk
├── TypeAdapterConfigExtensions.cs
└── icon.png
├── Mapster.JsonNet.Tests
├── JsonMappingTest.cs
└── Mapster.JsonNet.Tests.csproj
├── Mapster.JsonNet
├── JsonAdapter.cs
├── Mapster.JsonNet.csproj
├── Mapster.JsonNet.snk
├── TypeAdapterConfigExtensions.cs
└── icon.png
├── Mapster.SourceGenerator
├── Mapster.SourceGenerator.csproj
├── Mapster.SourceGenerator.snk
└── icon.png
├── Mapster.Tests
├── Classes
│ ├── Address.cs
│ ├── Customer.cs
│ ├── Product.cs
│ └── TypeTestClass.cs
├── Diagnostics
│ ├── DebugInfoInjectorEx.cs
│ ├── GlobalReference.cs
│ └── TypeAdapterConfigExtensions.cs
├── DynamicTypeGeneratorTests.cs
├── Mapster.Tests.csproj
├── Mapster.Tests.snk
├── Properties
│ └── AssemblyInfo.cs
├── WhenAddingCustomMappings.cs
├── WhenCloningConfig.cs
├── WhenCompilingConfig.cs
├── WhenConfiguringMapping.cs
├── WhenConvertingFromObjects.cs
├── WhenCreatingConfigInstance.cs
├── WhenExplicitMappingRequired.cs
├── WhenFlattening.cs
├── WhenForkingConfig.cs
├── WhenHandlingUnmappedMembers.cs
├── WhenIgnoreMapping.cs
├── WhenIgnoreWithAttribute.cs
├── WhenIgnoringConditionally.cs
├── WhenIgnoringNonMapped.cs
├── WhenIncludeDerivedClasses.cs
├── WhenMappingArrays.cs
├── WhenMappingCollections.cs
├── WhenMappingComplexClasses.cs
├── WhenMappingConditionally.cs
├── WhenMappingDerived.cs
├── WhenMappingEntityWithOnlyPrimitives.cs
├── WhenMappingEnums.cs
├── WhenMappingErrorThrown.cs
├── WhenMappingIEnumerableClass.cs
├── WhenMappingIgnoreNullValues.cs
├── WhenMappingMultipleSources.cs
├── WhenMappingNonPublicSetters.cs
├── WhenMappingNullablePrimitives.cs
├── WhenMappingPrimitives.cs
├── WhenMappingPrivateFieldsAndProperties.cs
├── WhenMappingRecordTypes.cs
├── WhenMappingStruct.cs
├── WhenMappingStructRegression.cs
├── WhenMappingToConstructor.cs
├── WhenMappingToConstructorAndPrivateSetters.cs
├── WhenMappingToInterface.cs
├── WhenMappingToTarget.cs
├── WhenMappingWithAdaptIgnoreRegression.cs
├── WhenMappingWithDictionary.cs
├── WhenMappingWithExplicitInheritance.cs
├── WhenMappingWithExtensionMethods.cs
├── WhenMappingWithFlexibleName.cs
├── WhenMappingWithGetMethod.cs
├── WhenMappingWithIReadOnlyDictionary.cs
├── WhenMappingWithImplicitInheritance.cs
├── WhenMappingWithInstance.cs
├── WhenMappingWithOpenGenerics.cs
├── WhenMappingWithParametersOnRecordsRegression.cs
├── WhenMappingWithPath.cs
├── WhenMappingWithSecondSourceObject.cs
├── WhenPassingRuntimeValue.cs
├── WhenPerformingAfterMapping.cs
├── WhenPerformingBeforeMapping.cs
├── WhenPerformingDestinationTransforms.cs
├── WhenPreserveReferences.cs
├── WhenProjecting.cs
├── WhenRegisteringAndMappingRace.cs
├── WhenRunningOnMultipleThreads.cs
├── WhenScanningForRegisters.cs
├── WhenSettingMapToTargetWith.cs
├── WhenUnflattening.cs
├── WhenUsingAttribute.cs
├── WhenUsingAttributeWithNameMatchingStrategy.cs
├── WhenUsingConverterFactory.cs
├── WhenUsingDestinationValue.cs
├── WhenUsingIMapFrom.cs
├── WhenUsingIMapFromWrong.cs
├── WhenUsingMaxDepth.cs
├── WhenUsingNonDefaultConstructor.cs
├── WhenUsingRuleBasedSetting.cs
└── mock.keys
├── Mapster.Tool.Tests
├── .config
│ └── dotnet-tools.json
├── Mappers
│ ├── IUserMapper.cs
│ └── UserMapper.cs
├── Mapster.Tool.Tests.csproj
├── TestBase.cs
├── Usings.cs
└── WhenMappingWithExistingObjectAndInitProperties.cs
├── Mapster.Tool
├── ExtensionOptions.cs
├── Extensions.cs
├── MapperOptions.cs
├── Mapster.Tool.csproj
├── Mapster.Tool.snk
├── MockType.cs
├── ModelOptions.cs
├── Program.cs
├── Properties
│ └── launchSettings.json
└── icon.png
├── Mapster.sln
├── Mapster
├── Adapters
│ ├── ArrayAdapter.cs
│ ├── BaseAdapter.cs
│ ├── BaseAfterMapper.cs
│ ├── BaseClassAdapter.cs
│ ├── ClassAdapter.cs
│ ├── CollectionAdapter.cs
│ ├── DelegateAdapter.cs
│ ├── DictionaryAdapter.cs
│ ├── EnumAdapter.cs
│ ├── MultiDimensionalArrayAdapter.cs
│ ├── ObjectAdapter.cs
│ ├── ObjectType.cs
│ ├── PrimitiveAdapter.cs
│ ├── RecordTypeAdapter.cs
│ └── StringAdapter.cs
├── Compile
│ ├── CompileArgument.cs
│ ├── CompileContext.cs
│ ├── CompileException.cs
│ └── PreCompileArgument.cs
├── Enums
│ ├── AccessModifier.cs
│ └── EnumMappingStrategy.cs
├── Extensions.cs
├── Interfaces
│ ├── IAdapterBuilder.cs
│ ├── IApplyable.cs
│ ├── IMapFrom.cs
│ ├── IMapper.cs
│ ├── IRegister.cs
│ └── ITypeAdapterBuilder.cs
├── Mapper.cs
├── Mapster.csproj
├── Mapster.csproj.DotSettings
├── Mapster.nuget.targets
├── Mapster.snk
├── Models
│ ├── ClassMapping.cs
│ ├── ClassModel.cs
│ ├── FieldModel.cs
│ ├── IMemberModel.cs
│ ├── IMemberModelEx.cs
│ ├── InvokerModel.cs
│ ├── KeyValuePairModel.cs
│ ├── MemberMapping.cs
│ ├── ParameterModel.cs
│ ├── PropertyModel.cs
│ └── TypeTuple.cs
├── Properties
│ └── AssemblyInfo.cs
├── Settings
│ ├── DestinationTransform.cs
│ ├── GetMemberName.cs
│ ├── IgnoreDictionary.cs
│ ├── NameMatchingStrategy.cs
│ ├── SettingStore.cs
│ ├── ShouldMapMember.cs
│ ├── UseDestinationValue.cs
│ └── ValueAccessingStrategy.cs
├── TypeAdapter.cs
├── TypeAdapterBuilder.cs
├── TypeAdapterConfig.cs
├── TypeAdapterRule.cs
├── TypeAdapterSetter.cs
├── TypeAdapterSettings.cs
├── Utils
│ ├── BlockExpressionDetector.cs
│ ├── ComplexExpressionVisitor.cs
│ ├── CoreExtensions.cs
│ ├── CustomAttributeUtil.cs
│ ├── DynamicTypeGenerator.cs
│ ├── ExpressionEx.cs
│ ├── InterfaceDynamicMapper.cs
│ ├── NullableExpressionVisitor.cs
│ ├── ParameterExpressionReplacer.cs
│ ├── ReflectionUtils.cs
│ └── TypeAdapterConfigExtensions.cs
└── icon.png
├── Pack.bat
├── Publish.bat
├── Sample.AspNetCore
├── Controllers
│ └── SchoolController.cs
├── DbInitializer.cs
├── Models
│ ├── Course.cs
│ ├── Enrollment.cs
│ ├── SchoolContext.cs
│ └── Student.cs
├── NameFormatter.cs
├── Program.cs
├── Properties
│ └── launchSettings.json
├── Sample.AspNetCore.csproj
├── Startup.cs
├── appsettings.Development.json
└── appsettings.json
├── Sample.CodeGen
├── Controllers
│ └── SchoolController.cs
├── DbInitializer.cs
├── Domains
│ ├── Course.cs
│ ├── Enrollment.cs
│ ├── SchoolContext.cs
│ └── Student.cs
├── Mappers
│ ├── IStudentMapper.cs
│ └── StudentMapper.g.cs
├── MappingRegister.cs
├── Models
│ ├── CourseAdd.g.cs
│ ├── CourseDto.g.cs
│ ├── CourseMapper.g.cs
│ ├── CourseMerge.g.cs
│ ├── CourseUpdate.g.cs
│ ├── EnrollmentAdd.g.cs
│ ├── EnrollmentDto.g.cs
│ ├── EnrollmentMerge.g.cs
│ ├── EnrollmentUpdate.g.cs
│ ├── Person.cs
│ ├── StudentAdd.g.cs
│ ├── StudentDto.g.cs
│ ├── StudentMapper.g.cs
│ ├── StudentMerge.g.cs
│ └── StudentUpdate.g.cs
├── Program.cs
├── Properties
│ └── launchSettings.json
├── Sample.CodeGen.csproj
├── Startup.cs
├── appsettings.Development.json
└── appsettings.json
└── TemplateTest
├── CreateMapExpressionTest.cs
├── FooTest.cs
└── TemplateTest.csproj
/.config/dotnet-tools.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": 1,
3 | "isRoot": true,
4 | "tools": {
5 | "mapster.tool": {
6 | "version": "8.1.0",
7 | "commands": [
8 | "dotnet-mapster"
9 | ]
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/.github/workflows/dotnet-buildandtest.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a .NET project
2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
3 |
4 | name: Build and Test
5 |
6 | on:
7 | push:
8 | branches: [ "master", "main", "development", "**" ]
9 | pull_request:
10 | branches: [ "master", "main", "development" ]
11 | jobs:
12 | build:
13 | runs-on: ubuntu-latest
14 | steps:
15 | - uses: actions/checkout@v3
16 | - name: Setup .NET
17 | uses: actions/setup-dotnet@v3
18 | with:
19 | dotnet-version: 7.0.x
20 | - name: Show dotnet version
21 | run: |
22 | dotnet --list-sdks
23 | dotnet --list-runtimes
24 | - name: Build with dotnet
25 | run: dotnet build ./src/Mapster.sln
26 | - name: Run tests on .NET 7.0
27 | run: dotnet test --verbosity normal ./src/Mapster.sln
28 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Chaowlert Chaisrichalermpol, Eric Swann
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/.editorconfig:
--------------------------------------------------------------------------------
1 | [*.cs]
2 |
3 | # S3220: Method calls should not resolve ambiguously to overloads with "params"
4 | dotnet_diagnostic.S3220.severity = suggestion
5 |
6 | # RCS1238: Avoid nested ?: operators.
7 | dotnet_diagnostic.RCS1238.severity = suggestion
8 |
9 | # S3358: Ternary operators should not be nested
10 | dotnet_diagnostic.S3358.severity = suggestion
11 |
12 | # S125: Sections of code should not be commented out
13 | dotnet_diagnostic.S125.severity = suggestion
14 |
15 | # RCS1146: Use conditional access.
16 | dotnet_diagnostic.RCS1146.severity = suggestion
17 |
--------------------------------------------------------------------------------
/src/Benchmark/Benchmark.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | true
7 | **/*.g.cs
8 |
9 |
10 |
11 | True
12 | True
13 | CustomerMapper.tt
14 |
15 |
16 | True
17 | True
18 | FooMapper.tt
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | TextTemplatingFileGenerator
36 | CustomerMapper.g.cs
37 |
38 |
39 | TextTemplatingFileGenerator
40 | FooMapper.g.cs
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/Benchmark/Benchmarks/Config.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Columns;
2 | using BenchmarkDotNet.Configs;
3 | using BenchmarkDotNet.Diagnosers;
4 | using BenchmarkDotNet.Exporters;
5 | using BenchmarkDotNet.Exporters.Csv;
6 | using BenchmarkDotNet.Jobs;
7 | using BenchmarkDotNet.Loggers;
8 |
9 | namespace Benchmark.Benchmarks
10 | {
11 | public class Config : ManualConfig
12 | {
13 | public Config()
14 | {
15 | AddLogger(ConsoleLogger.Default);
16 |
17 | AddExporter(CsvExporter.Default);
18 | AddExporter(MarkdownExporter.GitHub);
19 | AddExporter(HtmlExporter.Default);
20 |
21 | AddDiagnoser(MemoryDiagnoser.Default);
22 | AddColumn(TargetMethodColumn.Method);
23 |
24 | AddColumn(StatisticColumn.Mean);
25 | AddColumn(StatisticColumn.StdDev);
26 | AddColumn(StatisticColumn.Error);
27 |
28 | AddColumn(BaselineRatioColumn.RatioMean);
29 | AddColumnProvider(DefaultColumnProviders.Metrics);
30 |
31 | AddJob(Job.ShortRun
32 | .WithLaunchCount(1)
33 | .WithWarmupCount(2)
34 | .WithIterationCount(10)
35 | );
36 |
37 | Options |= ConfigOptions.JoinSummary;
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/src/Benchmark/Classes/Customer.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Benchmark.Classes
4 | {
5 | public class Address
6 | {
7 | public int Id { get; set; }
8 | public string Street { get; set; }
9 | public string City { get; set; }
10 | public string Country { get; set; }
11 | }
12 |
13 | public class AddressDTO
14 | {
15 | public int Id { get; set; }
16 | public string City { get; set; }
17 | public string Country { get; set; }
18 | }
19 |
20 | public class Customer
21 | {
22 | public int Id { get; set; }
23 | public string Name { get; set; }
24 | public decimal? Credit { get; set; }
25 | public Address Address { get; set; }
26 | public Address HomeAddress { get; set; }
27 | public Address[] Addresses { get; set; }
28 | public ICollection
WorkAddresses { get; set; }
29 | }
30 |
31 | public class CustomerDTO
32 | {
33 | public int Id { get; set; }
34 | public string Name { get; set; }
35 | public Address Address { get; set; }
36 | public AddressDTO HomeAddress { get; set; }
37 | public AddressDTO[] Addresses { get; set; }
38 | public List WorkAddresses { get; set; }
39 | public string AddressCity { get; set; }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Benchmark/Classes/Foo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Benchmark.Classes
5 | {
6 | public class Foo
7 | {
8 | public string Name { get; set; }
9 |
10 | public int Int32 { get; set; }
11 |
12 | public long Int64 { set; get; }
13 |
14 | public int? NullInt { get; set; }
15 |
16 | public float Floatn { get; set; }
17 |
18 | public double Doublen { get; set; }
19 |
20 | public DateTime DateTime { get; set; }
21 |
22 | public Foo Foo1 { get; set; }
23 |
24 | public IEnumerable Foos { get; set; }
25 |
26 | public Foo[] FooArr { get; set; }
27 |
28 | public int[] IntArr { get; set; }
29 |
30 | public IEnumerable Ints { get; set; }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Benchmark/CustomerMapper.tt:
--------------------------------------------------------------------------------
1 | <#@ template debug="true" language="C#" #>
2 | <#@ output extension=".g.cs" #>
3 | <# /*@ Assembly Name="/usr/local/share/dotnet/sdk/2.2.103/Microsoft/Microsoft.NET.Build.Extensions/net461/lib/netstandard.dll" */ #>
4 | <#@ Assembly Name="netstandard" #>
5 | <#@ Assembly Name="System.Core" #>
6 | <#@ Assembly Name="System.Runtime" #>
7 | <#@ Assembly Name="System.Linq.Expressions" #>
8 | <#@ Assembly Name="$(TargetDir)/$(ProjectName).dll" #>
9 | <#@ Assembly Name="$(TargetDir)/Mapster.dll" #>
10 | <#@ Assembly Name="$(TargetDir)/ExpressionTranslator.dll" #>
11 | <#@ import namespace="Benchmark.Classes" #>
12 | <#@ import namespace="ExpressionDebugger" #>
13 | <#@ import namespace="Mapster" #>
14 | <#
15 | TypeAdapterConfig.GlobalSettings.SelfContainedCodeGeneration = true;
16 | var foo = default(Customer);
17 | var def = new ExpressionDefinitions
18 | {
19 | IsStatic = true,
20 | MethodName = "Map",
21 | Namespace = "Benchmark",
22 | TypeName = "CustomerMapper"
23 | };
24 | var code = foo.BuildAdapter()
25 | .CreateMapExpression()
26 | .ToScript(def);
27 | WriteLine(code);
28 | #>
--------------------------------------------------------------------------------
/src/Benchmark/FooMapper.g.cs:
--------------------------------------------------------------------------------
1 |
2 | using System;
3 | using System.Linq;
4 | using Benchmark.Classes;
5 | using Mapster;
6 | using Mapster.Utils;
7 |
8 |
9 | namespace Benchmark
10 | {
11 | public static partial class FooMapper
12 | {
13 | public static Foo Map(Foo p1)
14 | {
15 | return p1 == null ? null : new Foo()
16 | {
17 | Name = p1.Name,
18 | Int32 = p1.Int32,
19 | Int64 = p1.Int64,
20 | NullInt = p1.NullInt,
21 | Floatn = p1.Floatn,
22 | Doublen = p1.Doublen,
23 | DateTime = p1.DateTime,
24 | Foo1 = Map(p1.Foo1),
25 | Foos = p1.Foos == null ? null : p1.Foos.Select(func1),
26 | FooArr = func2(p1.FooArr),
27 | IntArr = func3(p1.IntArr),
28 | Ints = p1.Ints == null ? null : MapsterHelper.ToEnumerable(p1.Ints)
29 | };
30 | }
31 |
32 | private static Foo func1(Foo p2)
33 | {
34 | return Map(p2);
35 | }
36 |
37 | private static Foo[] func2(Foo[] p3)
38 | {
39 | if (p3 == null)
40 | {
41 | return null;
42 | }
43 | Foo[] result = new Foo[p3.Length];
44 |
45 | int v = 0;
46 |
47 | int i = 0;
48 | int len = p3.Length;
49 |
50 | while (i < len)
51 | {
52 | Foo item = p3[i];
53 | result[v++] = Map(item);
54 | i++;
55 | }
56 | return result;
57 |
58 | }
59 |
60 | private static int[] func3(int[] p4)
61 | {
62 | if (p4 == null)
63 | {
64 | return null;
65 | }
66 | int[] result = new int[p4.Length];
67 | Array.Copy(p4, 0, result, 0, p4.Length);
68 | return result;
69 |
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/Benchmark/FooMapper.tt:
--------------------------------------------------------------------------------
1 | <#@ template debug="true" language="C#" #>
2 | <#@ output extension=".g.cs" #>
3 | <# /*@ Assembly Name="/usr/local/share/dotnet/sdk/2.2.103/Microsoft/Microsoft.NET.Build.Extensions/net461/lib/netstandard.dll" */ #>
4 | <#@ Assembly Name="netstandard" #>
5 | <#@ Assembly Name="System.Core" #>
6 | <#@ Assembly Name="System.Runtime" #>
7 | <#@ Assembly Name="System.Linq.Expressions" #>
8 | <#@ Assembly Name="$(TargetDir)/Benchmark.dll" #>
9 | <#@ Assembly Name="$(TargetDir)/Mapster.dll" #>
10 | <#@ Assembly Name="$(TargetDir)/ExpressionTranslator.dll" #>
11 | <#@ import namespace="Benchmark.Classes" #>
12 | <#@ import namespace="ExpressionDebugger" #>
13 | <#@ import namespace="Mapster" #>
14 | <#
15 | TypeAdapterConfig.GlobalSettings.SelfContainedCodeGeneration = true;
16 | var foo = default(Foo);
17 | var def = new ExpressionDefinitions
18 | {
19 | IsStatic = true,
20 | MethodName = "Map",
21 | Namespace = "Benchmark",
22 | TypeName = "FooMapper"
23 | };
24 | var code = foo.BuildAdapter()
25 | .CreateMapExpression()
26 | .ToScript(def);
27 | code = code.Replace("TypeAdapter.Map.Invoke", "Map");
28 | WriteLine(code);
29 | #>
--------------------------------------------------------------------------------
/src/Benchmark/Program.cs:
--------------------------------------------------------------------------------
1 | using Benchmark.Benchmarks;
2 | using BenchmarkDotNet.Running;
3 |
4 | namespace Benchmark
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var switcher = new BenchmarkSwitcher(new[]
11 | {
12 | typeof(TestSimpleTypes),
13 | typeof(TestComplexTypes),
14 | typeof(TestAll),
15 | });
16 |
17 | switcher.Run(args, new Config());
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 |
6 |
7 |
8 | chaowlert;eric_swann;andrerav
9 | Copyright (c) $([System.DateTime]::Now.ToString(`yyyy`)) Chaowlert Chaisrichalermpol, Eric Swann, Andreas Ravnestad
10 | false
11 | https://github.com/MapsterMapper/Mapster
12 | https://github.com/MapsterMapper/Mapster
13 |
14 | logo.png
15 | https://cloud.githubusercontent.com/assets/5763993/26522718/d16f3e42-4330-11e7-9b78-f8c7402624e7.png
16 | MIT
17 | true
18 | $(SolutionDir)/Mapster/Mapster.snk
19 | true
20 | Mapper;AutoMapper;Fast;Mapping
21 | icon.png
22 | false
23 | 10
24 |
25 |
--------------------------------------------------------------------------------
/src/ExpressionDebugger.Console/ExpressionDebugger.Console.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0;net7.0
6 | true
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/ExpressionDebugger.Console/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq.Expressions;
3 |
4 | namespace ExpressionDebugger.Console
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var p1 = Expression.Parameter(typeof(int));
11 | var p2 = Expression.Parameter(typeof(int));
12 | var body = Expression.Add(p1, Expression.Block(
13 | new Expression[] {
14 | Expression.Call(typeof(System.Console).GetMethod("WriteLine", new [] { typeof(int) }), p2),
15 | p2,
16 | }));
17 | var lambda = Expression.Lambda>(body, p1, p2);
18 |
19 | var script = lambda.ToScript();
20 |
21 | var fun = lambda.CompileWithDebugInfo();
22 | var result = fun(1, 2);
23 | System.Console.WriteLine(result);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/ExpressionDebugger.Tests/ExpressionDebugger.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0;net7.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/ExpressionDebugger/ExpressionCompilationOptions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Reflection;
3 |
4 | namespace ExpressionDebugger
5 | {
6 | public class ExpressionCompilationOptions
7 | {
8 | public ExpressionDefinitions? DefaultDefinitions { get; set; }
9 | public IEnumerable? References { get; set; }
10 | public bool EmitFile { get; set; }
11 | public string? RootPath { get; set; }
12 | public bool? IsRelease { get; set; }
13 | public bool ThrowOnFailedCompilation { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/ExpressionDebugger/ExpressionDebugger.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0;net7.0
5 | True
6 | Chaowlert Chaisrichalermpol
7 | Step into debugging from linq expressions
8 | https://github.com/chaowlert/ExpressionDebugger
9 | https://github.com/chaowlert/ExpressionDebugger
10 | expression;linq;debug
11 | https://cloud.githubusercontent.com/assets/5763993/26522656/41e28a6e-432f-11e7-9cae-7856f927d1a1.png
12 | True
13 | true
14 | ExpressionDebugger.snk
15 | 2.2.0
16 | https://github.com/chaowlert/ExpressionDebugger/blob/master/LICENSE
17 | 8.0
18 | enable
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/src/ExpressionDebugger/ExpressionDebugger.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/ExpressionDebugger/ExpressionDebugger.snk
--------------------------------------------------------------------------------
/src/ExpressionTranslator/ExpressionDefinitions.cs:
--------------------------------------------------------------------------------
1 | namespace ExpressionDebugger
2 | {
3 | public class ExpressionDefinitions : TypeDefinitions
4 | {
5 | public string? MethodName { get; set; }
6 | public bool IsExpression { get; set; }
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/ExpressionTranslator/ExpressionTranslator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0;net7.0
5 | True
6 | Chaowlert Chaisrichalermpol
7 | Translate from linq expressions to C# code
8 | https://github.com/chaowlert/ExpressionDebugger
9 | https://github.com/chaowlert/ExpressionDebugger
10 | expression;linq;debug
11 | https://cloud.githubusercontent.com/assets/5763993/26522656/41e28a6e-432f-11e7-9cae-7856f927d1a1.png
12 | True
13 | true
14 | ExpressionTranslator.snk
15 | 2.4.3
16 | ExpressionDebugger
17 | MIT
18 | icon.png
19 |
20 | 8.0
21 | enable
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/ExpressionTranslator/ExpressionTranslator.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/ExpressionTranslator/ExpressionTranslator.snk
--------------------------------------------------------------------------------
/src/ExpressionTranslator/ExpressionTranslatorExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq.Expressions;
3 |
4 | namespace ExpressionDebugger
5 | {
6 | public static class ExpressionTranslatorExtensions
7 | {
8 | ///
9 | /// Generate script text
10 | ///
11 | public static string ToScript(this Expression node, ExpressionDefinitions? definitions = null)
12 | {
13 | var translator = ExpressionTranslator.Create(node, definitions);
14 | return translator.ToString();
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/ExpressionTranslator/Extensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Text;
6 |
7 | namespace ExpressionDebugger
8 | {
9 | internal static class Extensions
10 | {
11 | public static HashSet ToHashSet(this IEnumerable source)
12 | {
13 | return new HashSet(source);
14 | }
15 |
16 | #if NET40
17 | public static Type GetTypeInfo(this Type type) {
18 | return type;
19 | }
20 | #endif
21 |
22 | #if NET40 || NETSTANDARD1_3 || NET6_0_OR_GREATER
23 | public static T GetCustomAttribute(this MemberInfo memberInfo) where T : Attribute
24 | {
25 | return (T)memberInfo.GetCustomAttributes(typeof(T), true).SingleOrDefault();
26 | }
27 |
28 | public static T GetCustomAttribute(this Type type) where T : Attribute
29 | {
30 | return (T)type.GetTypeInfo().GetCustomAttributes(typeof(T), true).SingleOrDefault();
31 | }
32 | #endif
33 |
34 | public static int FindStartIndex(this StringBuilder sb)
35 | {
36 | int wsCount = 0;
37 | for (int i = 0; i < sb.Length; i++)
38 | {
39 | if (char.IsWhiteSpace(sb[i]))
40 | wsCount++;
41 | else
42 | break;
43 | }
44 | return wsCount;
45 | }
46 |
47 | public static int FindEndIndex(this StringBuilder sb)
48 | {
49 | int wsCount = 0;
50 | for (int i = sb.Length - 1; i >= 0; i--)
51 | {
52 | if (char.IsWhiteSpace(sb[i]))
53 | wsCount++;
54 | else
55 | break;
56 | }
57 | return sb.Length - wsCount;
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/ExpressionTranslator/PropertyDefinitions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection;
4 |
5 | namespace ExpressionDebugger
6 | {
7 | public class PropertyDefinitions
8 | {
9 | public Type Type { get; set; }
10 | public string Name { get; set; }
11 | public bool IsReadOnly { get; set; }
12 | public bool IsInitOnly { get; set; }
13 |
14 | ///
15 | /// Set to 2 to mark type as nullable
16 | ///
17 | public byte? NullableContext { get; set; }
18 |
19 | ///
20 | /// If type is generic, array or tuple, you can mark nullable for each type
21 | /// Set to 2 for nullable
22 | ///
23 | public byte[]? Nullable { get; set; }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/ExpressionTranslator/TypeDefinitions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace ExpressionDebugger
5 | {
6 | public class TypeDefinitions
7 | {
8 | public string? Namespace { get; set; }
9 | public string? TypeName { get; set; }
10 | public bool IsStatic { get; set; }
11 | public bool IsInternal { get; set; }
12 | public IEnumerable? Implements { get; set; }
13 | public bool PrintFullTypeName { get; set; }
14 | public bool IsRecordType { get; set; }
15 |
16 | ///
17 | /// Set to 2 to mark all properties as nullable
18 | ///
19 | public byte? NullableContext { get; set; }
20 | }
21 | }
--------------------------------------------------------------------------------
/src/ExpressionTranslator/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/ExpressionTranslator/icon.png
--------------------------------------------------------------------------------
/src/Mapster.Async.Tests/Mapster.Async.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/Mapster.Async/Mapster.Async.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 | Async supports for Mapster
6 | true
7 | Mapster;Async
8 | true
9 | Mapster.Async.snk
10 | 2.0.1-pre02
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/Mapster.Async/Mapster.Async.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.Async/Mapster.Async.snk
--------------------------------------------------------------------------------
/src/Mapster.Async/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.Async/icon.png
--------------------------------------------------------------------------------
/src/Mapster.Core/Attributes/AdaptIgnoreAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Mapster
4 | {
5 | [AttributeUsage(AttributeTargets.Field
6 | | AttributeTargets.Property)]
7 | public class AdaptIgnoreAttribute : Attribute
8 | {
9 | public MemberSide? Side { get; set; }
10 |
11 | public AdaptIgnoreAttribute() { }
12 |
13 | public AdaptIgnoreAttribute(MemberSide side)
14 | {
15 | this.Side = side;
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Attributes/AdaptMemberAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Mapster
4 | {
5 | [AttributeUsage(AttributeTargets.Field
6 | | AttributeTargets.Parameter
7 | | AttributeTargets.Property)]
8 | public class AdaptMemberAttribute : Attribute
9 | {
10 | public string? Name { get; set; }
11 | public MemberSide? Side { get; set; }
12 |
13 | public AdaptMemberAttribute() { }
14 | public AdaptMemberAttribute(string name)
15 | {
16 | this.Name = name;
17 | }
18 | public AdaptMemberAttribute(string name, MemberSide side)
19 | {
20 | this.Name = name;
21 | this.Side = side;
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Attributes/GenerateMapperAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Mapster
4 | {
5 | [AttributeUsage(AttributeTargets.Class
6 | | AttributeTargets.Struct
7 | | AttributeTargets.Interface)]
8 | public class GenerateMapperAttribute : Attribute
9 | {
10 | public string Name { get; set; } = "[name]Mapper";
11 | public Type[]? ForAttributes { get; set; }
12 | public bool IsHelperClass { get; set; }
13 | public bool IsInternal { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Attributes/MapperAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Mapster
4 | {
5 | [AttributeUsage(AttributeTargets.Interface)]
6 | public class MapperAttribute : Attribute
7 | {
8 | public string? Name { get; set; }
9 | public bool IsInternal { get; set; }
10 | }
11 | }
--------------------------------------------------------------------------------
/src/Mapster.Core/Attributes/PropertyTypeAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Mapster
4 | {
5 | [AttributeUsage(AttributeTargets.Class
6 | | AttributeTargets.Struct
7 | | AttributeTargets.Interface
8 | | AttributeTargets.Property
9 | | AttributeTargets.Field, AllowMultiple = true)]
10 | public class PropertyTypeAttribute : Attribute
11 | {
12 | public Type Type { get; }
13 | public Type[]? ForAttributes { get; set; }
14 |
15 | public PropertyTypeAttribute(Type type)
16 | {
17 | this.Type = type;
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Attributes/UseDestinationValueAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Mapster
4 | {
5 | [AttributeUsage(AttributeTargets.Field
6 | | AttributeTargets.Property)]
7 | public class UseDestinationValueAttribute : Attribute
8 | {
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Enums/MapType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Mapster
4 | {
5 | [Flags]
6 | public enum MapType
7 | {
8 | Map = 1,
9 | MapToTarget = 2,
10 | Projection = 4,
11 | }
12 | }
--------------------------------------------------------------------------------
/src/Mapster.Core/Enums/MemberSide.cs:
--------------------------------------------------------------------------------
1 | namespace Mapster
2 | {
3 | public enum MemberSide
4 | {
5 | Source,
6 | Destination,
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/Mapster.Core/MapContext/MapContext.cs:
--------------------------------------------------------------------------------
1 | using Mapster.Utils;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Threading;
5 | namespace Mapster
6 | {
7 | ///
8 | /// This class is to send data between mapping process
9 | ///
10 | ///
11 | /// The idea of this class is similar to Transaction & TransactionScope
12 | /// You can get context by MapContext.Current
13 | /// And all mapping processes will having only one context
14 | ///
15 | public class MapContext
16 | {
17 | #if (NETSTANDARD || NET6_0_OR_GREATER) && !MAPSTER_FORCE_LEGACY_MAPCONTEXT
18 | private static readonly AsyncLocal _localContext = new AsyncLocal();
19 | public static MapContext? Current
20 | {
21 | get => _localContext.Value;
22 | set => _localContext.Value = value;
23 | }
24 | #else
25 | [field: ThreadStatic]
26 | public static MapContext? Current { get; set; }
27 | #endif
28 |
29 | private Dictionary? _references;
30 | public Dictionary References => _references ??= new Dictionary();
31 |
32 | private Dictionary? _parameters;
33 | public Dictionary Parameters => _parameters ??= new Dictionary();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Mapster.Core/MapContext/MapContextScope.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Mapster.Utils;
3 |
4 | namespace Mapster
5 | {
6 | public class MapContextScope : IDisposable
7 | {
8 | public static MapContextScope Required()
9 | {
10 | return new MapContextScope();
11 | }
12 |
13 | public static MapContextScope RequiresNew()
14 | {
15 | return new MapContextScope(true);
16 | }
17 |
18 | public MapContext Context { get; }
19 |
20 | private readonly MapContext? _previousContext;
21 |
22 | public MapContextScope() : this(false) { }
23 | public MapContextScope(bool ignorePreviousContext)
24 | {
25 | _previousContext = MapContext.Current;
26 |
27 | this.Context = ignorePreviousContext
28 | ? new MapContext()
29 | : _previousContext ?? new MapContext();
30 |
31 | MapContext.Current = this.Context;
32 | }
33 |
34 | public void Dispose()
35 | {
36 | MapContext.Current = _previousContext;
37 | }
38 |
39 | public static TResult GetOrAddMapReference(ReferenceTuple key, Func mapFn) where TResult : notnull
40 | {
41 | using var context = new MapContextScope();
42 | var dict = context.Context.References;
43 | if (!dict.TryGetValue(key, out var reference))
44 | dict[key] = reference = mapFn(key);
45 | return (TResult)reference;
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Mapster.Core.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | Lightweight library for Mapster and Mapster CodeGen
4 | net7.0;net6.0
5 | Mapster.Core
6 | mapster
7 | 1.2.1-pre04
8 | enable
9 | true
10 | true
11 | Mapster.Core.snk
12 | Mapster
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Mapster.Core.csproj.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | True
3 | True
4 | True
5 | True
--------------------------------------------------------------------------------
/src/Mapster.Core/Mapster.Core.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.Core/Mapster.Core.snk
--------------------------------------------------------------------------------
/src/Mapster.Core/Models/Never.cs:
--------------------------------------------------------------------------------
1 | namespace Mapster.Models
2 | {
3 | public struct Never { }
4 | }
5 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Register/CodeGenerationConfig.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace Mapster
4 | {
5 | public class CodeGenerationConfig
6 | {
7 | public List AdaptAttributeBuilders { get; } = new List();
8 | public List GenerateMapperAttributeBuilders { get; } = new List();
9 | public AdaptAttributeBuilder Default { get; } = new AdaptAttributeBuilder(new AdaptFromAttribute("void"));
10 |
11 | public AdaptAttributeBuilder AdaptTo(string name, MapType? mapType = null)
12 | {
13 | var builder = new AdaptAttributeBuilder(new AdaptToAttribute(name) {MapType = mapType ?? 0});
14 | AdaptAttributeBuilders.Add(builder);
15 | return builder;
16 | }
17 |
18 | public AdaptAttributeBuilder AdaptFrom(string name, MapType? mapType = null)
19 | {
20 | var builder = new AdaptAttributeBuilder(new AdaptFromAttribute(name) {MapType = mapType ?? 0});
21 | AdaptAttributeBuilders.Add(builder);
22 | return builder;
23 | }
24 |
25 | public AdaptAttributeBuilder AdaptTwoWays(string name, MapType? mapType = null)
26 | {
27 | var builder = new AdaptAttributeBuilder(new AdaptTwoWaysAttribute(name) {MapType = mapType ?? 0});
28 | AdaptAttributeBuilders.Add(builder);
29 | return builder;
30 | }
31 |
32 | public GenerateMapperAttributeBuilder GenerateMapper(string name)
33 | {
34 | var builder = new GenerateMapperAttributeBuilder(new GenerateMapperAttribute());
35 | GenerateMapperAttributeBuilders.Add(builder);
36 | return builder;
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/src/Mapster.Core/Register/GenerateMapperAttributeBuilder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 |
6 | namespace Mapster
7 | {
8 | public class GenerateMapperAttributeBuilder
9 | {
10 | public GenerateMapperAttribute Attribute { get; }
11 | public HashSet Types { get; } = new HashSet();
12 |
13 | public GenerateMapperAttributeBuilder(GenerateMapperAttribute attribute)
14 | {
15 | this.Attribute = attribute;
16 | }
17 |
18 | public GenerateMapperAttributeBuilder ForTypes(params Type[] types)
19 | {
20 | this.Types.UnionWith(types);
21 | return this;
22 | }
23 |
24 | public GenerateMapperAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @namespace)
25 | {
26 | this.Types.UnionWith(
27 | assembly.GetTypes()
28 | .Where(it => (it.Namespace == @namespace || it.Namespace?.StartsWith(@namespace + '.') == true) &&
29 | !it.Name.Contains('<')));
30 | return this;
31 | }
32 |
33 | public GenerateMapperAttributeBuilder ForType()
34 | {
35 | this.Types.Add(typeof(T));
36 | return this;
37 | }
38 |
39 | public GenerateMapperAttributeBuilder ExcludeTypes(params Type[] types)
40 | {
41 | this.Types.ExceptWith(types);
42 | return this;
43 | }
44 |
45 | public GenerateMapperAttributeBuilder ExcludeTypes(Predicate predicate)
46 | {
47 | this.Types.RemoveWhere(predicate);
48 | return this;
49 | }
50 |
51 | }
52 | }
--------------------------------------------------------------------------------
/src/Mapster.Core/Register/ICodeGenerationRegister.cs:
--------------------------------------------------------------------------------
1 | namespace Mapster
2 | {
3 | public interface ICodeGenerationRegister
4 | {
5 | void Register(CodeGenerationConfig config);
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Register/PropertySetting.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq.Expressions;
3 |
4 | namespace Mapster
5 | {
6 | public class PropertySetting
7 | {
8 | public bool Ignore { get; set; }
9 | public string? TargetPropertyName { get; set; }
10 | public Type? TargetPropertyType { get; set; }
11 | public LambdaExpression? MapFunc { get; set; }
12 | }
13 | }
--------------------------------------------------------------------------------
/src/Mapster.Core/Utils/Extensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Linq.Expressions;
5 | using System.Reflection;
6 |
7 | namespace Mapster.Utils
8 | {
9 | public static class Extensions
10 | {
11 | #if NET40
12 | public static Type GetTypeInfo(this Type type)
13 | {
14 | return type;
15 | }
16 | #endif
17 |
18 | public static string GetMemberName(this LambdaExpression lambda)
19 | {
20 | string? prop = null;
21 | var expr = lambda.Body;
22 | if (expr.NodeType == ExpressionType.MemberAccess)
23 | {
24 | var memEx = (MemberExpression)expr;
25 | prop = memEx.Member.Name;
26 | expr = (Expression?)memEx.Expression;
27 | }
28 | if (prop == null || expr?.NodeType != ExpressionType.Parameter)
29 | throw new ArgumentException("Allow only first level member access (eg. obj => obj.Name)", nameof(lambda));
30 | return prop;
31 | }
32 |
33 | public static IEnumerable GetLoadableTypes(this Assembly assembly)
34 | {
35 | try
36 | {
37 | return assembly.GetTypes();
38 | }
39 | catch (ReflectionTypeLoadException e)
40 | {
41 | return e.Types.Where(t => t != null).Cast();
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Mapster.Core/Utils/ReferenceTuple.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.CompilerServices;
3 |
4 | namespace Mapster.Utils
5 | {
6 | public readonly struct ReferenceTuple : IEquatable
7 | {
8 | public object Reference { get; }
9 | public Type DestinationType { get; }
10 | public ReferenceTuple(object reference, Type destinationType)
11 | {
12 | this.Reference = reference;
13 | this.DestinationType = destinationType;
14 | }
15 |
16 | public override bool Equals(object obj)
17 | {
18 | return obj is ReferenceTuple other && Equals(other);
19 | }
20 |
21 | public bool Equals(ReferenceTuple other)
22 | {
23 | return ReferenceEquals(this.Reference, other.Reference)
24 | && this.DestinationType == other.DestinationType;
25 | }
26 |
27 | public override int GetHashCode()
28 | {
29 | unchecked
30 | {
31 | return (RuntimeHelpers.GetHashCode(this.Reference) * 397) ^ DestinationType.GetHashCode();
32 | }
33 | }
34 |
35 | public static bool operator ==(ReferenceTuple left, ReferenceTuple right)
36 | {
37 | return left.Equals(right);
38 | }
39 |
40 | public static bool operator !=(ReferenceTuple left, ReferenceTuple right)
41 | {
42 | return !(left == right);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Mapster.Core/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.Core/icon.png
--------------------------------------------------------------------------------
/src/Mapster.DependencyInjection.Tests/Mapster.DependencyInjection.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 | Dependency Injection supports for Mapster
6 | true
7 | Mapster;DependencyInjection
8 | true
9 | Mapster.DependencyInjection.snk
10 | 1.0.1-pre02
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/Mapster.DependencyInjection/Mapster.DependencyInjection.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.DependencyInjection/Mapster.DependencyInjection.snk
--------------------------------------------------------------------------------
/src/Mapster.DependencyInjection/ServiceCollectionExtensions.cs:
--------------------------------------------------------------------------------
1 | using MapsterMapper;
2 | using Microsoft.Extensions.DependencyInjection;
3 |
4 | namespace Mapster
5 | {
6 | public static class ServiceCollectionExtensions
7 | {
8 | public static void AddMapster(this IServiceCollection serviceCollection)
9 | {
10 | serviceCollection.AddTransient();
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/src/Mapster.DependencyInjection/TypeAdapterExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using MapsterMapper;
4 |
5 | namespace Mapster
6 | {
7 | public static class TypeAdapterExtensions
8 | {
9 | internal static U GetValueOrDefault(this IDictionary dict, T key)
10 | {
11 | return dict.TryGetValue(key, out var value) ? value : default;
12 | }
13 |
14 | public static TService GetService(this MapContext context)
15 | {
16 | var sp = (IServiceProvider) context?.Parameters.GetValueOrDefault(ServiceMapper.DI_KEY);
17 | if (sp == null)
18 | throw new InvalidOperationException("Mapping must be called using ServiceAdapter");
19 | return (TService)sp.GetService(typeof(TService));
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Mapster.DependencyInjection/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.DependencyInjection/icon.png
--------------------------------------------------------------------------------
/src/Mapster.EF6/Mapster.EF6.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 | EF6 plugin for Mapster
6 | true
7 | Mapster;EF6
8 | True
9 | true
10 | Mapster.EF6.snk
11 | 2.0.1-pre02
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/Mapster.EF6/Mapster.EF6.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.EF6/Mapster.EF6.snk
--------------------------------------------------------------------------------
/src/Mapster.EF6/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.EF6/icon.png
--------------------------------------------------------------------------------
/src/Mapster.EF6/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/Mapster.EFCore.Tests/Mapster.EFCore.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | all
16 | runtime; build; native; contentfiles; analyzers; buildtransitive
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/Mapster.EFCore.Tests/Models/Course.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.ComponentModel.DataAnnotations.Schema;
3 |
4 | namespace Mapster.EFCore.Tests.Models
5 | {
6 | public class Course
7 | {
8 | [DatabaseGenerated(DatabaseGeneratedOption.None)]
9 | public int CourseID { get; set; }
10 | public string Title { get; set; }
11 | public int Credits { get; set; }
12 |
13 | public ICollection Enrollments { get; set; }
14 | }}
15 |
--------------------------------------------------------------------------------
/src/Mapster.EFCore.Tests/Models/Enrollment.cs:
--------------------------------------------------------------------------------
1 | namespace Mapster.EFCore.Tests.Models
2 | {
3 | public enum Grade
4 | {
5 | A, B, C, D, F
6 | }
7 |
8 | public class Enrollment
9 | {
10 | public int EnrollmentID { get; set; }
11 | public int CourseID { get; set; }
12 | public int StudentID { get; set; }
13 | public Grade? Grade { get; set; }
14 |
15 | public Course Course { get; set; }
16 | public Student Student { get; set; }
17 | }}
18 |
--------------------------------------------------------------------------------
/src/Mapster.EFCore.Tests/Models/SchoolContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 |
3 | namespace Mapster.EFCore.Tests.Models
4 | {
5 | public class SchoolContext : DbContext
6 | {
7 | public SchoolContext(DbContextOptions options) : base(options)
8 | {
9 | }
10 |
11 | public DbSet Courses { get; set; }
12 | public DbSet Enrollments { get; set; }
13 | public DbSet Students { get; set; }
14 | }}
15 |
--------------------------------------------------------------------------------
/src/Mapster.EFCore.Tests/Models/Student.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Mapster.EFCore.Tests.Models
5 | {
6 | public class Student
7 | {
8 | public int ID { get; set; }
9 | public string LastName { get; set; }
10 | public string FirstMidName { get; set; }
11 | public DateTime EnrollmentDate { get; set; }
12 |
13 | public ICollection Enrollments { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/Mapster.EFCore/Mapster.EFCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 | EFCore plugin for Mapster
6 | true
7 | Mapster;EFCore
8 | True
9 | true
10 | Mapster.EFCore.snk
11 | 5.1.1-pre02
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/Mapster.EFCore/Mapster.EFCore.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.EFCore/Mapster.EFCore.snk
--------------------------------------------------------------------------------
/src/Mapster.EFCore/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.EFCore/icon.png
--------------------------------------------------------------------------------
/src/Mapster.EFCore/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/Mapster.Immutable.Tests/ImmutableTest.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Collections.Immutable;
3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
4 | using Shouldly;
5 |
6 | namespace Mapster.Immutable.Tests
7 | {
8 | [TestClass]
9 | public class TestImmutable
10 | {
11 | [TestMethod]
12 | public void TestImmutableArray()
13 | {
14 | var config = new TypeAdapterConfig();
15 | config.EnableImmutableMapping();
16 |
17 | var list = new[] {1, 2, 3, 4};
18 | var array = list.Adapt>(config);
19 | array.ShouldBe(list);
20 | }
21 |
22 | [TestMethod]
23 | public void TestImmutableDictionary()
24 | {
25 | var config = new TypeAdapterConfig();
26 | config.EnableImmutableMapping();
27 |
28 | var poco = new {Name = "Foo", Id = "Bar"};
29 | var dict = poco.Adapt>(config);
30 | dict["Name"].ShouldBe(poco.Name);
31 | dict["Id"].ShouldBe(poco.Id);
32 | }
33 |
34 | [TestMethod]
35 | public void TestImmutableDictionary2()
36 | {
37 | var config = new TypeAdapterConfig();
38 | config.EnableImmutableMapping();
39 |
40 | var list = new Dictionary
41 | {
42 | [1] = 2,
43 | [3] = 4,
44 | };
45 | var dict = list.Adapt>(config);
46 | dict.ShouldBe(list);
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Mapster.Immutable.Tests/Mapster.Immutable.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | all
15 | runtime; build; native; contentfiles; analyzers; buildtransitive
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/Mapster.Immutable/Mapster.Immutable.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 | Immutable collection supports for Mapster
6 | true
7 | Mapster;Immutable
8 | true
9 | Mapster.Immutable.snk
10 | 1.0.1-pre02
11 | enable
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/Mapster.Immutable/Mapster.Immutable.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.Immutable/Mapster.Immutable.snk
--------------------------------------------------------------------------------
/src/Mapster.Immutable/TypeAdapterConfigExtensions.cs:
--------------------------------------------------------------------------------
1 | using Mapster.Immutable;
2 |
3 | namespace Mapster
4 | {
5 | public static class TypeAdapterConfigExtensions
6 | {
7 | public static void EnableImmutableMapping(this TypeAdapterConfig config)
8 | {
9 | config.Rules.Add(new ImmutableAdapter().CreateRule());
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/Mapster.Immutable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.Immutable/icon.png
--------------------------------------------------------------------------------
/src/Mapster.JsonNet.Tests/JsonMappingTest.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 | using Newtonsoft.Json.Linq;
3 | using Shouldly;
4 |
5 | namespace Mapster.JsonNet.Tests
6 | {
7 | [TestClass]
8 | public class JsonMappingTest
9 | {
10 | private static TypeAdapterConfig _config;
11 |
12 | [ClassInitialize]
13 | public static void Setup(TestContext context)
14 | {
15 | _config = new TypeAdapterConfig();
16 | _config.EnableJsonMapping();
17 | }
18 |
19 | [TestMethod]
20 | public void JsonToJson()
21 | {
22 | var json = new JObject();
23 | var result = json.Adapt(_config);
24 | result.ShouldBe(json);
25 | }
26 |
27 | [TestMethod]
28 | public void FromString()
29 | {
30 | var str = @"{ ""foo"": ""bar"" }";
31 | var result = str.Adapt(_config);
32 | result["foo"].ShouldBe("bar");
33 | }
34 |
35 | [TestMethod]
36 | public void ToStringTest()
37 | {
38 | var json = new JObject {["foo"] = "bar"};
39 | var result = json.Adapt(_config);
40 | result.ShouldContainWithoutWhitespace(@"{""foo"":""bar""}");
41 | }
42 |
43 | [TestMethod]
44 | public void FromObject()
45 | {
46 | var obj = new Mock {foo = "bar"};
47 | var result = obj.Adapt(_config);
48 | result["foo"].ShouldBe("bar");
49 | }
50 |
51 | [TestMethod]
52 | public void ToObject()
53 | {
54 | var json = new JObject { ["foo"] = "bar" };
55 | var result = json.Adapt(_config);
56 | result.foo.ShouldBe("bar");
57 | }
58 | }
59 |
60 | public class Mock
61 | {
62 | public string foo { get; set; }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/Mapster.JsonNet.Tests/Mapster.JsonNet.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/Mapster.JsonNet/Mapster.JsonNet.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 | Json.net conversion supports for Mapster
6 | true
7 | Mapster;Json.net
8 | true
9 | Mapster.JsonNet.snk
10 | 1.1.1-pre03
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/Mapster.JsonNet/Mapster.JsonNet.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.JsonNet/Mapster.JsonNet.snk
--------------------------------------------------------------------------------
/src/Mapster.JsonNet/TypeAdapterConfigExtensions.cs:
--------------------------------------------------------------------------------
1 | using Mapster.JsonNet;
2 |
3 | namespace Mapster
4 | {
5 | public static class TypeAdapterConfigExtensions
6 | {
7 | public static void EnableJsonMapping(this TypeAdapterConfig config)
8 | {
9 | config.Rules.Add(new JsonAdapter().CreateRule());
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/Mapster.JsonNet/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.JsonNet/icon.png
--------------------------------------------------------------------------------
/src/Mapster.SourceGenerator/Mapster.SourceGenerator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0;net6.0
5 | Source generator to generate mapping using Mapster
6 | source-generator;mapster
7 | true
8 | Mapster.SourceGenerator.snk
9 | https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json ;$(RestoreAdditionalProjectSources)
10 | 6.5.1
11 | enable
12 | false
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/Mapster.SourceGenerator/Mapster.SourceGenerator.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.SourceGenerator/Mapster.SourceGenerator.snk
--------------------------------------------------------------------------------
/src/Mapster.SourceGenerator/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MapsterMapper/Mapster/bab634544ff7838c1770ad4db2f6d68c4f733a19/src/Mapster.SourceGenerator/icon.png
--------------------------------------------------------------------------------
/src/Mapster.Tests/Classes/Address.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations.Schema;
2 |
3 | namespace Mapster.Tests.Classes
4 | {
5 | [ComplexType]
6 | public class Address
7 | {
8 | public string Street { get; set; }
9 | public string ZipCode { get; set; }
10 | public string Country { get; set; }
11 | public string City { get; set; }
12 | public AddressType AddressType { get; set; }
13 | }
14 |
15 | public enum AddressType
16 | {
17 | Work = 1,
18 | Home = 2
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Mapster.Tests/Classes/Customer.cs:
--------------------------------------------------------------------------------
1 | namespace Mapster.Tests.Classes
2 | {
3 | public class Customer
4 | {
5 | public int Id { get; set; }
6 | public string Name { get; set; }
7 | public string Surname { get; set; }
8 | public Address Address { get; set; }
9 | }
10 |
11 | public class CustomerDTO
12 | {
13 | public int Id { get; set; }
14 | public string Name { get; set; }
15 | public string Address_Country { get; set; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Mapster.Tests/Diagnostics/DebugInfoInjectorEx.cs:
--------------------------------------------------------------------------------
1 | using ExpressionDebugger;
2 | using System;
3 | using System.IO;
4 | using System.Linq.Expressions;
5 | using System.Reflection;
6 | using System.Reflection.Emit;
7 |
8 | namespace Mapster.Diagnostics
9 | {
10 | public class DebugInfoInjectorEx: DebugInfoInjector
11 | {
12 | public DebugInfoInjectorEx(string filename): base (filename) { }
13 |
14 | public DebugInfoInjectorEx(TextWriter writer): base(writer) { }
15 |
16 | protected override Expression VisitConstant(ConstantExpression node)
17 | {
18 | node = (ConstantExpression)base.VisitConstant(node);
19 |
20 | if (CanEmitConstant(node.Value, node.Type))
21 | return node;
22 |
23 | return GetNonPublicObject(node.Value, node.Type);
24 | }
25 |
26 | private static Expression GetNonPublicObject(object value, Type type)
27 | {
28 | var i = GlobalReference.GetIndex(value);
29 | return Expression.Convert(
30 | Expression.Call(
31 | typeof(GlobalReference).GetMethod(nameof(GlobalReference.GetObject)),
32 | Expression.Constant(i)),
33 | type);
34 | }
35 |
36 | private static bool CanEmitConstant(object value, Type type)
37 | {
38 | if (value == null
39 | || type.IsPrimitive
40 | || type == typeof(string)
41 | || type == typeof(decimal))
42 | return true;
43 |
44 | if (value is Type t)
45 | return IsVisible(t);
46 |
47 | if (value is MethodBase mb)
48 | return IsVisible(mb);
49 |
50 | return false;
51 | }
52 |
53 | private static bool IsVisible(Type t)
54 | {
55 | return t is TypeBuilder
56 | || t.IsGenericParameter
57 | || t.IsVisible;
58 | }
59 |
60 | private static bool IsVisible(MethodBase mb)
61 | {
62 | if (mb is DynamicMethod || !mb.IsPublic)
63 | return false;
64 |
65 | Type dt = mb.DeclaringType;
66 | return dt == null || IsVisible(dt);
67 | }
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/src/Mapster.Tests/Diagnostics/GlobalReference.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace ExpressionDebugger
4 | {
5 | public static class GlobalReference
6 | {
7 | private static readonly List