ykafia / SoftTouch.Reflection

Simple C# source generators for reflection, avoids boxing and is AOT compatible

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SoftTouch.Reflection

A simple reflection source generator, avoids boxing and is AOT compatible.

First, types that should be reflected must be partial and have the Reflectable attribute.

using SoftTouch.Reflection.Core;

namespace SoftTouch.Reflection.Example;

[Reflectable]
public partial struct Person
{
    public string Name { get; set; }
    public int Age { get; }
    public House PersonHouse { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

[Reflectable]
public partial class House
{
    public string Address { get; }
    internal int Number { get; set; }

}

This will implement the interface IReflectable and add some methods and members to access and modify properties of the data.

using SoftTouch.Reflection.Example;
using SoftTouch.Reflection.Core;

static void Reflect<T>(ref T data)
    where T : struct, IReflectable
{
    data.Set("Name", "Jane");
    Console.WriteLine(data.Get<string>("Name"));
}


var person = new Person("John", 5);

foreach(var p in Person.Setters)
    Console.WriteLine(p);

Reflect(ref person);

About

Simple C# source generators for reflection, avoids boxing and is AOT compatible

License:MIT License


Languages

Language:C# 100.0%