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

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators