RSCG Example–ApparatusAOT–part 26
name | ApparatusAOT |
nuget |
https://www.nuget.org/packages/Apparatus.AOT.Reflection/ |
link | https://github.com/byme8/Apparatus.AOT.Reflection |
author | Stanislav Silin |
This will generate code for investigating at runtime the properties of an object
The code that you start with is
01 02 03 04 05 06 07 08 09 10 11 12 13 | //no special requirements class Person { [Required] public string FirstName { get ; set ; } public string LastName { get ; set ; } } |
The code that you will use is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | var pOldPerson = new Person(); pOldPerson.FirstName = "Andrei" ; var prop = pOldPerson.GetProperties().Values ; foreach ( var item in prop) { Console.WriteLine($ "{item.Name} Attr: {item.Attributes.Length} value {item.Name}" ); if (item.TryGetValue(pOldPerson, out var val)){ Console.WriteLine( "value : " + val); } } |
The code that is generated is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | using System; using System.Linq; namespace Apparatus.AOT.Reflection { public static class CopyConstructor_PersonExtensions { [global::System.Runtime.CompilerServices.ModuleInitializer] public static void Bootstrap() { MetadataStore<global::CopyConstructor.Person>.Data = _lazy; } private static global::System.Lazy<global::System.Collections.Generic.IReadOnlyDictionary< string , IPropertyInfo>> _lazy = new global::System.Lazy<global::System.Collections.Generic.IReadOnlyDictionary< string , IPropertyInfo>>( new global::System.Collections.Generic.Dictionary< string , IPropertyInfo> { { "FirstName" , new global::Apparatus.AOT.Reflection.PropertyInfo<global::CopyConstructor.Person, string >( "FirstName" , new global::System.Attribute[] { new global::System.ComponentModel.DataAnnotations.RequiredAttribute(), }, instance => instance.FirstName, (instance, value) => instance.FirstName = value) }, { "LastName" , new global::Apparatus.AOT.Reflection.PropertyInfo<global::CopyConstructor.Person, string >( "LastName" , new global::System.Attribute[] { }, instance => instance.LastName, (instance, value) => instance.LastName = value) }, }); internal static global::System.Collections.Generic.IReadOnlyDictionary< string , IPropertyInfo> GetProperties( this global::CopyConstructor.Person value) { return _lazy.Value; } } } |
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/ApparatusAOT
Leave a Reply