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
//no special requirements class Person { [Required] public string FirstName { get; set; } public string LastName { get; set; } }
The code that you will use is
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
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