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

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