Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Cricle/StaticReflection
Use roslyn for static reflection
https://github.com/Cricle/StaticReflection
Last synced: 2 months ago
JSON representation
Use roslyn for static reflection
- Host: GitHub
- URL: https://github.com/Cricle/StaticReflection
- Owner: Cricle
- Created: 2023-05-21T14:18:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-14T08:50:19.000Z (over 1 year ago)
- Last Synced: 2024-05-22T04:32:23.401Z (8 months ago)
- Language: C#
- Size: 240 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- RSCG_Examples - https://github.com/Cricle/StaticReflection/
README
StaticReflection
A fast, easy, scalable static reflection.## Fast use
* Install from nuget `FastStaticReflection`, `FastStaticReflection.CodeGen`
* Write assembly class
```csharp
[StaticReflectionAssembly]//for generate assembly code
public partial class C
{
}
```* Tag static type reflection
```csharp
//You can Tag at assembly
[assembly: StaticReflection(Type = typeof(StaticReflection.Sample.A))]//Or Property
[StaticReflection]
[StaticReflection(Type =typeof(B))]
public A a { get; set; }//Or class
[StaticReflection]
public class A
{
//....
}
```* For use
```csharp
internal class Program
{
static void Main(string[] args)
{
var b=new Student();
var @class=C.Default.Types.First(x => x.Name == "Student");
@class.SetProperty(b, "Id", 1);//Reflection get property value
Console.WriteLine("Id: "[email protected](b, "Id"));//Reflection set property value
var @event = (IEventTransfer)@class.Events.First(x => x.Name == "AlreadyGoSchool");
using (var eventScope = @event.CreateScope(b))
{
eventScope.Start();
eventScope.EventTransfed += Instance_EventTransfed;//Reflection listen event
var method = @class.Methods.First(x => x.Name == "GoToSchool");
Console.WriteLine("GoToSchool:" + method.InvokeUsualMethod(b));//Reflection call method
}
var obj = @class.Constructors.First(x => x.ArgumentTypes.Count == 0);
var inst = obj.InvokeUsualMethod(null);//Reflection create object
Console.WriteLine(inst);
}private static void Instance_EventTransfed(object? sender, EventTransferEventArgs e)
{
Console.WriteLine("EventRaise: " + e.Args[0]);
}
}
[StaticReflection]
public record class Student
{
public int Id { get; set; }public string? Name { get; set; }
public event EventHandler? AlreadyGoSchool;
public int GoToSchool()
{
AlreadyGoSchool?.Invoke(this, this);
return Id;
}
}
[StaticReflectionAssembly]
public partial class C
{
}```
## Benchmarks
[Benchmarks](./test/Benchmarks.md)