├── MapDataReader.Tests ├── Usings.cs ├── MapDataReader.Tests.csproj ├── TestGenerator.cs └── TestActualCode.cs ├── .github └── workflows │ └── dotnet.yml ├── MapDataReader.Benchmarks ├── MapDataReader.Benchmarks.csproj └── Program.cs ├── LICENSE ├── MapDataReader ├── MapDataReader.csproj └── MapperGenerator.cs ├── MapDataReader.sln ├── README.md └── .gitignore /MapDataReader.Tests/Usings.cs: -------------------------------------------------------------------------------- 1 | global using Microsoft.VisualStudio.TestTools.UnitTesting; -------------------------------------------------------------------------------- /.github/workflows/dotnet.yml: -------------------------------------------------------------------------------- 1 | name: .NET 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Setup .NET 17 | uses: actions/setup-dotnet@v3 18 | with: 19 | dotnet-version: 6.0.x 20 | - name: Restore dependencies 21 | run: dotnet restore 22 | - name: Build 23 | run: dotnet build --no-restore 24 | - name: Test 25 | run: dotnet test --no-build --verbosity normal 26 | -------------------------------------------------------------------------------- /MapDataReader.Benchmarks/MapDataReader.Benchmarks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MapDataReader.Tests/MapDataReader.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Jitbit (the company behind "Jitbit Helpdesk" software) 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 | -------------------------------------------------------------------------------- /MapDataReader/MapDataReader.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | MapDataReader 6 | MapDataReader 7 | Alex from Jitbit 8 | MapDataReader 9 | https://github.com/jitbit/mapdatareader 10 | README.md 11 | https://github.com/jitbit/mapdatareader 12 | LICENSE 13 | 1.0.13 14 | aot;source-generator 15 | Super fast mapping of DataReader to custom objects 16 | True 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | True 29 | \ 30 | 31 | 32 | True 33 | \ 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /MapDataReader.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32922.545 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MapDataReader", "MapDataReader\MapDataReader.csproj", "{8D4B7D66-48DA-4B9B-AF0F-26B1F73DFCDF}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MapDataReader.Tests", "MapDataReader.Tests\MapDataReader.Tests.csproj", "{B75A0108-AC68-4534-BA28-C27C4DF1CF71}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapDataReader.Benchmarks", "MapDataReader.Benchmarks\MapDataReader.Benchmarks.csproj", "{3B6CABDA-B42B-4471-AA08-FEB536DE4F12}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {8D4B7D66-48DA-4B9B-AF0F-26B1F73DFCDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {8D4B7D66-48DA-4B9B-AF0F-26B1F73DFCDF}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {8D4B7D66-48DA-4B9B-AF0F-26B1F73DFCDF}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {8D4B7D66-48DA-4B9B-AF0F-26B1F73DFCDF}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {B75A0108-AC68-4534-BA28-C27C4DF1CF71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {B75A0108-AC68-4534-BA28-C27C4DF1CF71}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {B75A0108-AC68-4534-BA28-C27C4DF1CF71}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {B75A0108-AC68-4534-BA28-C27C4DF1CF71}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {3B6CABDA-B42B-4471-AA08-FEB536DE4F12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {3B6CABDA-B42B-4471-AA08-FEB536DE4F12}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {3B6CABDA-B42B-4471-AA08-FEB536DE4F12}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {3B6CABDA-B42B-4471-AA08-FEB536DE4F12}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {DF179183-4E2D-4583-8432-259EEB9BA165} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /MapDataReader.Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using BenchmarkDotNet.Running; 3 | using Dapper; 4 | using System.Data; 5 | using System.Reflection; 6 | 7 | namespace MapDataReader.Benchmarks 8 | { 9 | internal class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | BenchmarkRunner.Run(); 14 | } 15 | } 16 | 17 | [ShortRunJob, MemoryDiagnoser] 18 | public class Benchy 19 | { 20 | static TestClass _o = new TestClass(); 21 | static PropertyInfo _prop = _o.GetType().GetProperty("String1", BindingFlags.Public | BindingFlags.Instance); 22 | static PropertyInfo _nullableprop = _o.GetType().GetProperty("IntNullable", BindingFlags.Public | BindingFlags.Instance); 23 | 24 | [Benchmark] 25 | public void SetProp_Reflection() 26 | { 27 | PropertyInfo prop = _o.GetType().GetProperty("String1", BindingFlags.Public | BindingFlags.Instance); 28 | if (null != prop && prop.CanWrite) 29 | { 30 | prop.SetValue(_o, "Value"); 31 | } 32 | } 33 | 34 | [Benchmark] 35 | public void SetProp_ReflectionCached() 36 | { 37 | _prop.SetValue(_o, "Value"); 38 | } 39 | 40 | [Benchmark] 41 | public void SetProp_MapDataReader() 42 | { 43 | _o.SetPropertyByName("String1", "Value"); 44 | } 45 | 46 | [Benchmark] 47 | public void SetNullableProp_ReflectionCached() 48 | { 49 | _nullableprop.SetValue(_o, 123); 50 | } 51 | 52 | [Benchmark] 53 | public void SetNullableProp_MapDataReader() 54 | { 55 | _o.SetPropertyByName("IntNullable", 123); 56 | } 57 | 58 | [Benchmark] 59 | public void MapDatareader_ViaDapper() 60 | { 61 | var dr = _dt.CreateDataReader(); 62 | var list = dr.Parse().ToList(); 63 | } 64 | 65 | [Benchmark] 66 | public void MapDataReader_ViaMapaDataReader() 67 | { 68 | var dr = _dt.CreateDataReader(); 69 | var list = dr.ToTestClass(); 70 | } 71 | 72 | static DataTable _dt; 73 | 74 | [GlobalSetup] 75 | public static void Setup() 76 | { 77 | //create datatable with test data 78 | _dt = new DataTable(); 79 | _dt.Columns.AddRange(new[] { 80 | new DataColumn("String1", typeof(string)), 81 | new DataColumn("String2", typeof(string)), 82 | new DataColumn("String3", typeof(string)), 83 | new DataColumn("Int", typeof(int)), 84 | new DataColumn("Int2", typeof(int)), 85 | new DataColumn("IntNullable", typeof(int)) 86 | }); 87 | 88 | 89 | for (int i = 0; i < 1000; i++) 90 | { 91 | _dt.Rows.Add("xxx", "yyy", "zzz", 123, 321, 3211); 92 | } 93 | } 94 | } 95 | 96 | [GenerateDataReaderMapper] 97 | public class TestClass 98 | { 99 | public string String1 { get; set; } 100 | public string String2 { get; set; } 101 | public string String3 { get; set; } 102 | public string Int { get; set; } 103 | public string Int2 { get; set; } 104 | public int? IntNullable { get; set; } 105 | } 106 | } -------------------------------------------------------------------------------- /MapDataReader.Tests/TestGenerator.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp; 3 | using System.Collections.Immutable; 4 | using System.Data; 5 | using System.Diagnostics; 6 | using System.Reflection; 7 | 8 | namespace MapDataReader.Tests 9 | { 10 | [TestClass] 11 | public class TestGenerator 12 | { 13 | [TestMethod] 14 | public void TestGeneral() 15 | { 16 | string userSource = @" 17 | using MapDataReader; 18 | 19 | namespace MyCode 20 | { 21 | [GenerateDataReaderMapper] 22 | public class MyClass 23 | { 24 | public string Name {get;set;} 25 | public int Size {get;set;} 26 | public bool Enabled {get;set;} 27 | public System.DateTime Created {get;set;} 28 | public System.DateTimeOffset Offset {get;set;} 29 | public decimal Price {get;set;} 30 | } 31 | } 32 | "; 33 | var src = GetAndCheckOutputSource(userSource); 34 | } 35 | 36 | //gets generated source and also unit-tests for exceptions and empty diagnistics etc 37 | private string GetAndCheckOutputSource(string inputSource) 38 | { 39 | var generator = new MapperGenerator(); 40 | 41 | Compilation comp = CreateCompilation(inputSource); 42 | 43 | // Create the driver that will control the generation, passing in our generator 44 | GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); 45 | 46 | // Run the generation pass 47 | // (Note: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls) 48 | driver = driver.RunGeneratorsAndUpdateCompilation(comp, out var outputCompilation, out var diagnostics); 49 | 50 | // We can now assert things about the resulting compilation: 51 | Assert.IsTrue(diagnostics.IsEmpty); // there were no diagnostics created by the generators 52 | Assert.IsTrue(outputCompilation.SyntaxTrees.Count() == 2); // we have two syntax trees, the original 'user' provided one, and the one added by the generator 53 | var compDiag = outputCompilation.GetDiagnostics(); 54 | Assert.IsTrue(compDiag.IsEmpty); // verify the compilation with the added source has no diagnostics 55 | 56 | // Or we can look at the results directly: 57 | GeneratorDriverRunResult runResult = driver.GetRunResult(); 58 | 59 | // The runResult contains the combined results of all generators passed to the driver 60 | Assert.IsTrue(runResult.GeneratedTrees.Length == 1); 61 | Assert.IsTrue(runResult.Diagnostics.IsEmpty); 62 | 63 | // Or you can access the individual results on a by-generator basis 64 | GeneratorRunResult generatorResult = runResult.Results[0]; 65 | Assert.IsTrue(generatorResult.Generator == generator); 66 | Assert.IsTrue(generatorResult.Diagnostics.IsEmpty); 67 | Assert.IsTrue(generatorResult.GeneratedSources.Length == 1); 68 | Assert.IsTrue(generatorResult.Exception is null); 69 | 70 | //now actually return the source generated 71 | return generatorResult.GeneratedSources[0].SourceText.ToString(); 72 | } 73 | 74 | private static Compilation CreateCompilation(string source) 75 | => CSharpCompilation.Create("compilation", 76 | new[] { CSharpSyntaxTree.ParseText(source) }, 77 | new[] { MetadataReference.CreateFromFile(typeof(Binder).GetTypeInfo().Assembly.Location), 78 | MetadataReference.CreateFromFile(typeof(MapperGenerator).GetTypeInfo().Assembly.Location), 79 | MetadataReference.CreateFromFile(typeof(IDataReader).GetTypeInfo().Assembly.Location), 80 | MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location), 81 | MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(object).Assembly.Location), "System.Runtime.dll")), 82 | MetadataReference.CreateFromFile(AppDomain.CurrentDomain.GetAssemblies().Single(a => a.GetName().Name == "netstandard").Location) 83 | }, 84 | new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); 85 | } 86 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MapDataReader 2 | Super fast mapping DataReader to a strongly typed object. High performance, lighweight (12Kb dll), uses AOT source generation and no reflection, mapping code is generated at compile time. 3 | 4 | [![.NET](https://github.com/jitbit/MapDataReader/actions/workflows/dotnet.yml/badge.svg)](https://github.com/jitbit/MapDataReader/actions/workflows/dotnet.yml) 5 | [![Nuget](https://img.shields.io/nuget/v/MapDataReader)](https://www.nuget.org/packages/MapDataReader/) 6 | ![Net stanrdard 2.0](https://img.shields.io/badge/netstandard-2.0-brightgreen) 7 | 8 | ## Benchmarks 9 | 10 | 20X faster than using reflection, even with caching. Benchmark for a tiny class with 5 string properties: 11 | 12 | | Method | Mean | Error | StdDev | Gen0 | Allocated | 13 | |--------------- |----------:|----------:|---------:|-------:|----------:| 14 | | Reflection | 951.16 ns | 15.107 ns | 0.828 ns | 0.1459 | 920 B | 15 | | MapDataReader | 44.15 ns | 2.840 ns | 0.156 ns | 0.0089 | 56 B | 16 | 17 | ## Install via [Nuget](https://www.nuget.org/packages/MapDataReader/) 18 | 19 | ``` 20 | Install-Package MapDataReader 21 | ``` 22 | 23 | ## Usage with `IDataReader` 24 | 25 | ```csharp 26 | using MapDataReader; 27 | 28 | [GenerateDataReaderMapper] // <-- mark your class with this attribute 29 | public class MyClass 30 | { 31 | public int ID { get; set; } 32 | public string Name { get; set; } 33 | public int Size { get; set; } 34 | public bool Enabled { get; set; } 35 | } 36 | 37 | var dataReader = new SqlCommand("SELECT * FROM MyTable", connection).ExecuteReader(); 38 | 39 | List results = dataReader.ToMyClass(); // "ToMyClass" method is generated at compile time 40 | ``` 41 | 42 | Some notes for the above 43 | 44 | * The `ToMyClass()` method above - is an `IDataReader` extension method generated at compile time. You can even "go to definition" in Visual Studio and examine its code. 45 | * The naming convention is `ToCLASSNAME()` we can't use generics here, since `` is not part of method signatures in C# (considered in later versions of C#). If you find a prettier way - please contribute! 46 | * Maps properies with public setters only. 47 | * The datareader is being closed after mapping, so don't reuse it. 48 | * Supports `enum` properties based on `int` and other implicit casting (sometimes a DataReader may decide to return `byte` for small integer database value, and it maps to `int` perfectly via some unboxing magic) 49 | * Properly maps `DBNull` to `null`. 50 | * Complex-type properties may not work. 51 | 52 | ## Bonus API: `SetPropertyByName` 53 | 54 | This package also adds a super fast `SetPropertyByName` extension method generated at compile time for your class. 55 | 56 | Usage: 57 | 58 | ```csharp 59 | var x = new MyClass(); 60 | x.SetPropertyByName("Size", 42); //20X faster than using reflection 61 | ``` 62 | 63 | | Method | Mean | Error | StdDev | Allocated | 64 | |------------------------ |----------:|----------:|----------:|----------:| 65 | | SetPropReflection | 98.294 ns | 5.7443 ns | 0.3149 ns | - | 66 | | SetPropReflectionCached | 71.137 ns | 1.9736 ns | 0.1082 ns | - | 67 | | SetPropMapDataReader | 4.711 ns | 0.4640 ns | 0.0254 ns | - | 68 | 69 | --- 70 | 71 | ## Tip: Using it with Dapper 72 | 73 | If you're already using the awesome [Dapper ORM](https://github.com/DapperLib/Dapper) by Marc Gravel, Sam Saffron and Nick Craver, this is how you can use our library to speed up DataReader-to-object mapping in Dapper: 74 | 75 | ```csharp 76 | // override Dapper extension method to use fast MapDataReader instead of Dapper's built-in reflection 77 | public static List Query(this SqlConnection cn, string sql, object parameters = null) 78 | { 79 | if (typeof(T) == typeof(MyClass)) //our own class that we marked with attribute? 80 | return cn.ExecuteReader(sql, parameters).ToMyClass() as List; //use MapDataReader 81 | 82 | if (typeof(T) == typeof(AnotherClass)) //another class we have enabled? 83 | return cn.ExecuteReader(sql, parameters).ToAnotherClass() as List; //again 84 | 85 | //fallback to Dapper by default 86 | return SqlMapper.Query(cn, sql, parameters).AsList(); 87 | } 88 | ``` 89 | Why the C# compiler will choose your method over Dapper's? 90 | 91 | When the C# compiler sees two extension methods with the same signature, it uses the one that's "closer" to your code. "Closiness" - is determined by multiple factors - same namespace, same assembly, derived class over base class, implementation over interface etc. Adding an override like this will silently switch your existing code from using Dapper/reflection to using our source generator (b/c it uses a more specific connection type and lives in your project's namescape), while still keeping the awesomeness of Dapper and you barely have to rewrite any of your code. 92 | 93 | --- 94 | 95 | ## P.S. But what's the point? 96 | 97 | While reflection-based ORMs like Dapper are very fast after all the reflaction objects have been cached, they still do a lot of reflection-based heavy-lifting when you query the database *for the first time*. Which slows down application startup *significantly*. Which, in turn, can become a problem if you deploy the application multiple times a day. 98 | 99 | Or - if you run your ASP.NET Core app on IIS - this causes 503 errors during IIS recycles, see https://github.com/dotnet/aspnetcore/issues/41340 and faster app startup helps a lot. 100 | 101 | Also, reflection-caching causes memory pressure becasue of all the concurrent dictionaries used for caching. 102 | 103 | And even with all the caching, a simple straightforward code like `obj.x = y` will always be faster then looking up a cached delegate in a thousands-long dictionary by a string key and invoking it via reflection. 104 | 105 | Even if you don't care about the startup performance of your app, `MapDataReader` is still 5-7% faster than `Dapper` (note - we're not even using Dapper's command-cache store here, just the datareader parser, actual real world Dapper scenario will be even slower) 106 | 107 | | Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated | 108 | |---------------- |--------------:|--------------:|-------------:|-------:|-------:|----------:| 109 | | DapperWithCache | 142.09 us | 8,013.663 ns | 439.256 ns | 9.0332 | 1.2207 | 57472 B | 110 | | MapDataReader | 133.22 us | 28,679.198 ns | 1,572.004 ns | 9.0332 | 1.2207 | 57624 B | 111 | 112 | -------------------------------------------------------------------------------- /MapDataReader/MapperGenerator.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp; 3 | using Microsoft.CodeAnalysis.CSharp.Syntax; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Collections.Immutable; 7 | using System.Linq; 8 | 9 | namespace MapDataReader 10 | { 11 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 12 | public class GenerateDataReaderMapperAttribute : Attribute 13 | { 14 | } 15 | 16 | [Generator] 17 | public class MapperGenerator : ISourceGenerator 18 | { 19 | public void Execute(GeneratorExecutionContext context) 20 | { 21 | var targetTypeTracker = context.SyntaxContextReceiver as TargetTypeTracker; 22 | 23 | foreach (var typeNode in targetTypeTracker.TypesNeedingGening) 24 | { 25 | var typeNodeSymbol = context.Compilation 26 | .GetSemanticModel(typeNode.SyntaxTree) 27 | .GetDeclaredSymbol(typeNode); 28 | 29 | var allProperties = typeNodeSymbol.GetAllSettableProperties(); 30 | 31 | var src = $@" 32 | // 33 | #pragma warning disable 8019 //disable 'unnecessary using directive' warning 34 | using System; 35 | using System.Data; 36 | using System.Linq; 37 | using System.Collections.Generic; //to support List etc 38 | 39 | namespace MapDataReader 40 | {{ 41 | public static partial class MapperExtensions 42 | {{ 43 | public static void SetPropertyByName(this {typeNodeSymbol.FullName()} target, string name, object value) 44 | {{ 45 | SetPropertyByUpperName(target, name.ToUpperInvariant(), value); 46 | }} 47 | 48 | private static void SetPropertyByUpperName(this {typeNodeSymbol.FullName()} target, string name, object value) 49 | {{ 50 | {"\r\n" + allProperties.Select(p => 51 | { 52 | var pTypeName = p.Type.FullName(); 53 | 54 | if (p.Type.IsReferenceType) //ref types - just cast to property type 55 | { 56 | return $@" if (name == ""{p.Name.ToUpperInvariant()}"") {{ target.{p.Name} = value as {pTypeName}; return; }}"; 57 | } 58 | else if (pTypeName.EndsWith("?") && !p.Type.IsNullableEnum()) //nullable type (unless nullable Enum) 59 | { 60 | var nonNullableTypeName = pTypeName.TrimEnd('?'); 61 | 62 | //do not use "as" operator becasue "as" is slow for nullable types. Use "is" and a null-check 63 | return $@" if (name == ""{p.Name.ToUpperInvariant()}"") {{ if(value==null) target.{p.Name}=null; else if(value is {nonNullableTypeName}) target.{p.Name}=({nonNullableTypeName})value; return; }}"; 64 | } 65 | else if (p.Type.TypeKind == TypeKind.Enum || p.Type.IsNullableEnum()) //enum? pre-convert to underlying type then to int, you can't cast a boxed int to enum directly. Also to support assigning "smallint" database col to int32 (for example), which does not work at first (you can't cast a boxed "byte" to "int") 66 | { 67 | return $@" if (value != null && name == ""{p.Name.ToUpperInvariant()}"") {{ target.{p.Name} = ({pTypeName})(value.GetType() == typeof(int) ? (int)value : (int)Convert.ChangeType(value, typeof(int))); return; }}"; //pre-convert enums to int first (after unboxing, see below) 68 | } 69 | else //primitive types. use Convert.ChangeType before casting. To support assigning "smallint" database col to int32 (for example), which does not work at first (you can't cast a boxed "byte" to "int") 70 | { 71 | return $@" if (value != null && name == ""{p.Name.ToUpperInvariant()}"") {{ target.{p.Name} = value.GetType() == typeof({pTypeName}) ? ({pTypeName})value : ({pTypeName})Convert.ChangeType(value, typeof({pTypeName})); return; }}"; 72 | } 73 | }).StringConcat("\r\n") } 74 | 75 | 76 | }} //end method"; 77 | 78 | if (typeNodeSymbol.InstanceConstructors.Any(c => !c.Parameters.Any())) //has a constructor without parameters? 79 | { 80 | src += $@" 81 | 82 | public static List<{typeNodeSymbol.FullName()}> To{typeNode.Identifier}(this IDataReader dr) 83 | {{ 84 | var list = new List<{typeNodeSymbol.FullName()}>(); 85 | 86 | if (dr.Read()) 87 | {{ 88 | string[] columnNames = new string[dr.FieldCount]; 89 | 90 | for (int i = 0; i < columnNames.Length; i++) 91 | columnNames[i] = dr.GetName(i).ToUpperInvariant(); 92 | 93 | do 94 | {{ 95 | var result = new {typeNodeSymbol.FullName()}(); 96 | for (int i = 0; i < columnNames.Length; i++) 97 | {{ 98 | var value = dr[i]; 99 | if (value is DBNull) value = null; 100 | SetPropertyByUpperName(result, columnNames[i], value); 101 | }} 102 | list.Add(result); 103 | }} while (dr.Read()); 104 | }} 105 | dr.Close(); 106 | return list; 107 | }}"; 108 | } 109 | 110 | src += "\n}"; //end class 111 | src += "\n}"; //end namespace 112 | 113 | // Add the source code to the compilation 114 | context.AddSource($"{typeNodeSymbol.Name}DataReaderMapper.g.cs", src); 115 | } 116 | } 117 | 118 | public void Initialize(GeneratorInitializationContext context) 119 | { 120 | context.RegisterForSyntaxNotifications(() => new TargetTypeTracker()); 121 | } 122 | } 123 | 124 | internal class TargetTypeTracker : ISyntaxContextReceiver 125 | { 126 | public IImmutableList TypesNeedingGening = ImmutableList.Create(); 127 | 128 | public void OnVisitSyntaxNode(GeneratorSyntaxContext context) 129 | { 130 | if (context.Node is ClassDeclarationSyntax cdecl) 131 | if (cdecl.IsDecoratedWithAttribute("GenerateDataReaderMapper")) 132 | TypesNeedingGening = TypesNeedingGening.Add(cdecl); 133 | } 134 | } 135 | 136 | internal static class Helpers 137 | { 138 | internal static bool IsDecoratedWithAttribute(this TypeDeclarationSyntax cdecl, string attributeName) => 139 | cdecl.AttributeLists 140 | .SelectMany(x => x.Attributes) 141 | .Any(x => x.Name.ToString().Contains(attributeName)); 142 | 143 | 144 | internal static string FullName(this ITypeSymbol typeSymbol) => typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); 145 | 146 | internal static string StringConcat(this IEnumerable source, string separator) => string.Join(separator, source); 147 | 148 | // returns all properties with public setters 149 | internal static IEnumerable GetAllSettableProperties(this ITypeSymbol typeSymbol) 150 | { 151 | var result = typeSymbol 152 | .GetMembers() 153 | .Where(s => s.Kind == SymbolKind.Property).Cast() //get all properties 154 | .Where(p => p.SetMethod?.DeclaredAccessibility == Accessibility.Public) //has a public setter? 155 | .ToList(); 156 | 157 | //now get the base class 158 | var baseType = typeSymbol.BaseType; 159 | if (baseType != null) 160 | result.AddRange(baseType.GetAllSettableProperties()); //recursion 161 | 162 | return result; 163 | } 164 | 165 | //checks if type is a nullable num 166 | internal static bool IsNullableEnum(this ITypeSymbol symbol) 167 | { 168 | //tries to get underlying non-nullable type from nullable type 169 | //and then check if it's Enum 170 | if (symbol.NullableAnnotation == NullableAnnotation.Annotated 171 | && symbol is INamedTypeSymbol namedType 172 | && namedType.IsValueType 173 | && namedType.IsGenericType 174 | && namedType.ConstructedFrom?.ToDisplayString() == "System.Nullable" 175 | ) 176 | return namedType.TypeArguments[0].TypeKind == TypeKind.Enum; 177 | 178 | return false; 179 | } 180 | } 181 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /MapDataReader.Tests/TestActualCode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data; 4 | using System.Diagnostics.CodeAnalysis; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace MapDataReader.Tests 10 | { 11 | public enum MyEnum 12 | { 13 | FirstDude, 14 | SecondDude, 15 | Third, 16 | } 17 | 18 | [GenerateDataReaderMapper] 19 | public class MyObject 20 | { 21 | public int Id { get; set; } 22 | public bool LaBoolissimmo { get; set; } 23 | public byte ByteMyAss { get; set;} 24 | public sbyte AssByte { get; set;} 25 | public char SnapChar { get; set;} 26 | public decimal Decl { get; set;} 27 | public double DoubleKick { get; set;} 28 | public float Floating { get; set;} 29 | public uint What { get; set; } 30 | public nint ImBored { get; set; } 31 | public long LOOOOOoong { get; set; } 32 | public short Shrt {get;set;} 33 | public DateTime BirthDay { get; set; } 34 | public DateTime? NUllableBirthDay { get; set; } 35 | public TimeSpan Elapsed { get; set; } 36 | public Guid MyGuid { get; set; } 37 | 38 | 39 | public MyEnum Dude { get; set; } 40 | public MyEnum? NullableDude { get; set; } 41 | public string Name { get; set; } 42 | 43 | public int GetOnly { get; } = 123; //property without public setter! 44 | 45 | public byte[] ByeArray { get; set; } 46 | public int[] IntArray { get; set; } 47 | public string[] StringArray { get; set; } 48 | public long[] LongArray { get; set; } 49 | } 50 | 51 | [TestClass] 52 | public class TestActualCode 53 | { 54 | [TestMethod] 55 | public void TestPrimitiveTypesAssign() 56 | { 57 | var o = new MyObject(); 58 | o.SetPropertyByName("Id", 123); 59 | Assert.IsTrue(o.Id == 123); 60 | 61 | o.SetPropertyByName("Id", (byte)25); //test upcasting from byte 62 | Assert.IsTrue(o.Id == 25); 63 | 64 | o.SetPropertyByName("LaBoolissimmo", true); 65 | Assert.IsTrue(o.LaBoolissimmo); 66 | o.SetPropertyByName("ByteMyAss", (byte)123); 67 | Assert.IsTrue(o.ByteMyAss == 123); 68 | o.SetPropertyByName("ByteMyAss", 123); //lets try to stick an int in it 69 | Assert.IsTrue(o.ByteMyAss == 123); 70 | o.SetPropertyByName("AssByte", 123); 71 | Assert.IsTrue(o.AssByte == 123); 72 | o.SetPropertyByName("SnapChar", 123); 73 | Assert.IsTrue(o.SnapChar == 123); 74 | o.SetPropertyByName("Decl", 123); 75 | Assert.IsTrue(o.Decl == 123); 76 | o.SetPropertyByName("DoubleKick", 123); 77 | Assert.IsTrue(o.DoubleKick == 123); 78 | o.SetPropertyByName("Floating", 123); 79 | Assert.IsTrue(o.Floating == 123); 80 | o.SetPropertyByName("fLOAtInG", 123); //mess the casing, should still work 81 | Assert.IsTrue(o.Floating == 123); 82 | o.SetPropertyByName("What", 123); 83 | Assert.IsTrue(o.What == 123); 84 | o.SetPropertyByName("ImBored", (nint)123); 85 | Assert.IsTrue(o.ImBored == 123); 86 | o.SetPropertyByName("LOOOOOoong", 123); 87 | Assert.IsTrue(o.LOOOOOoong == 123); 88 | o.SetPropertyByName("Shrt", 123); 89 | Assert.IsTrue(o.Shrt == 123); 90 | o.SetPropertyByName("Elapsed", TimeSpan.FromSeconds(123)); 91 | Assert.IsTrue(o.Elapsed == TimeSpan.FromSeconds(123)); 92 | 93 | var guid = Guid.NewGuid(); 94 | o.SetPropertyByName("MyGuid", guid); 95 | Assert.IsTrue(o.MyGuid == guid); 96 | 97 | var dt = new DateTime(2022, 09, 09); 98 | o.SetPropertyByName("BirthDay", dt); 99 | Assert.IsTrue(o.BirthDay == dt); 100 | o.SetPropertyByName("NUllableBirthDay", dt); 101 | Assert.IsTrue(o.NUllableBirthDay == dt); 102 | 103 | //test nullable assign 104 | DateTime? ndt = null; 105 | ndt = new DateTime(2022, 10, 10); 106 | o.SetPropertyByName("NUllableBirthDay", ndt); 107 | Assert.IsTrue(o.NUllableBirthDay == ndt); 108 | 109 | o.SetPropertyByName("GetOnly", 321); //should not throw any exception, even though this property is not settable 110 | 111 | o.SetPropertyByName("ByeArray", new byte[3] { 1, 2, 3 }); 112 | Assert.IsTrue(o.ByeArray.SequenceEqual(new byte[3] { 1, 2, 3 })); 113 | o.SetPropertyByName("IntArray", new int[3] { 1, 2, 3 }); 114 | Assert.IsTrue(o.IntArray.SequenceEqual(new int[3] { 1, 2, 3 })); 115 | o.SetPropertyByName("StringArray", new [] { "1", "2", "3" }); 116 | Assert.IsTrue(o.StringArray.SequenceEqual(new[] { "1", "2", "3" })); 117 | o.SetPropertyByName("LongArray", new long[3] { 1, 2, 3 }); 118 | Assert.IsTrue(o.LongArray.SequenceEqual(new long[3] { 1, 2, 3 })); 119 | } 120 | 121 | [TestMethod] 122 | public void TestEnumAssign() 123 | { 124 | var o = new MyObject(); 125 | o.SetPropertyByName("Dude", 0); 126 | Assert.IsTrue(o.Dude == MyEnum.FirstDude); 127 | 128 | o.SetPropertyByName("Dude", 1); 129 | Assert.IsTrue(o.Dude == MyEnum.SecondDude); 130 | 131 | o.SetPropertyByName("Dude", (byte)2); //let's shove a BYTE in there! 132 | Assert.IsTrue(o.Dude == MyEnum.Third); //eat this, boxing!! 133 | 134 | o.SetPropertyByName("NullableDude", 1); 135 | Assert.IsTrue(o.NullableDude == MyEnum.SecondDude); 136 | 137 | o.SetPropertyByName("NullableDude", MyEnum.FirstDude); 138 | Assert.IsTrue(o.NullableDude == MyEnum.FirstDude); 139 | } 140 | 141 | [TestMethod] 142 | public void TestStringAssign() 143 | { 144 | var o = new MyObject(); 145 | o.SetPropertyByName("Name", "awsdkljfghsldkgjh"); 146 | Assert.IsTrue(o.Name == "awsdkljfghsldkgjh"); 147 | } 148 | 149 | [TestMethod] 150 | public void TestDatatReader() 151 | { 152 | //create datatable with test data 153 | var dt = new DataTable(); 154 | dt.Columns.AddRange(new[] { 155 | new DataColumn("ID", typeof(int)), 156 | new DataColumn("Name", typeof(string)), 157 | new DataColumn("LaBoolissimmo", typeof(bool)), 158 | new DataColumn("Floating", typeof(float)), 159 | new DataColumn("LOOOOOoong", typeof(long)), 160 | new DataColumn("BirthDay", typeof(DateTime)), 161 | new DataColumn("Elapsed", typeof(TimeSpan)), 162 | new DataColumn("ByeArray", typeof(byte[])), 163 | }); 164 | var date = new DateTime(2022, 09, 09); 165 | dt.Rows.Add(123, "ggg", true, 3213, 123, date, TimeSpan.FromSeconds(123), new byte[] { 3, 2, 1 }); 166 | dt.Rows.Add(3, "fgdk", false, 11123, 321, date, TimeSpan.FromSeconds(123), new byte[] { 5, 6, 7, 8 }); 167 | 168 | var list = dt.CreateDataReader().ToMyObject(); 169 | 170 | Assert.IsTrue(list.Count == 2); 171 | 172 | Assert.IsTrue(list[0].Id == 123); 173 | Assert.IsTrue(list[0].Name == "ggg"); 174 | Assert.IsTrue(list[0].LaBoolissimmo == true); 175 | Assert.IsTrue(list[0].Floating == 3213); 176 | Assert.IsTrue(list[0].LOOOOOoong == 123); 177 | Assert.IsTrue(list[0].BirthDay == date); 178 | Assert.IsTrue(list[0].Elapsed == TimeSpan.FromSeconds(123)); 179 | Assert.IsTrue(list[0].ByeArray.SequenceEqual(new byte[3] { 3, 2, 1 })); 180 | 181 | 182 | Assert.IsTrue(list[1].Id == 3); 183 | Assert.IsTrue(list[1].Name == "fgdk"); 184 | Assert.IsTrue(list[1].LaBoolissimmo == false); 185 | Assert.IsTrue(list[1].Floating == 11123); 186 | Assert.IsTrue(list[1].LOOOOOoong == 321); 187 | Assert.IsTrue(list[1].BirthDay == date); 188 | Assert.IsTrue(list[1].Elapsed == TimeSpan.FromSeconds(123)); 189 | Assert.IsTrue(list[1].ByeArray.SequenceEqual(new byte[4] { 5, 6, 7, 8 })); 190 | 191 | //now create datatable with different column order and test on the same code generator!!! 192 | var dt2 = new DataTable(); 193 | dt2.Columns.AddRange(new[] { 194 | new DataColumn("LaBoolissimmo", typeof(bool)), 195 | new DataColumn("Name", typeof(string)), 196 | new DataColumn("ID", typeof(int)), 197 | }); 198 | 199 | dt2.Rows.Add(true, "alex", 123); 200 | 201 | list = dt2.CreateDataReader().ToMyObject(); //should not throw exception 202 | 203 | Assert.IsTrue(list[0].Id == 123); 204 | Assert.IsTrue(list[0].Name == "alex"); 205 | Assert.IsTrue(list[0].LaBoolissimmo == true); 206 | } 207 | 208 | [TestMethod] 209 | public void TestBaseClassAssign() 210 | { 211 | var o = new ChildClass(); 212 | o.SetPropertyByName("Id", 123); 213 | o.SetPropertyByName("Name", "blahblah"); 214 | 215 | Assert.IsTrue(o.Id == 123); 216 | Assert.IsTrue(o.Name == "blahblah"); 217 | } 218 | 219 | [TestMethod] 220 | public void TestWrongProperty() 221 | { 222 | var o = new MyObject(); 223 | o.SetPropertyByName("NonExistingProperttyName", 123); //should not throw exception 224 | 225 | //now test wrong type 226 | o.Name = "lalala"; 227 | o.SetPropertyByName("Name", 123); //try to assign string prop to int 228 | Assert.IsTrue(o.Name == null); //wrong type. should be null 229 | } 230 | } 231 | 232 | public class BaseClass 233 | { 234 | public int Id { get; set; } 235 | } 236 | 237 | [GenerateDataReaderMapper] 238 | public class ChildClass : BaseClass 239 | { 240 | public string Name { get; set; } 241 | } 242 | } 243 | 244 | --------------------------------------------------------------------------------