RSCG – StaticReflection
RSCG – StaticReflection
name | StaticReflection |
nuget |
https://www.nuget.org/packages/FastStaticReflection/ https://www.nuget.org/packages/FastStaticReflection.CodeGen/ |
link | https://github.com/Cricle/StaticReflection/ |
author | Cricle |
Call prop/methods on classes
This is how you can use StaticReflection .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="FastStaticReflection" Version="1.0.0-preview.3" /> <PackageReference Include="FastStaticReflection.CodeGen" Version="1.0.0-preview.3" /> </ItemGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> </Project>
The code that you will use is
using StaticReflection; using StaticReflectionDemo; var p = new Person(); PersonReflection.Instance.SetProperty(p, "FirstName","Andrei"); PersonReflection.Instance.SetProperty(p, "LastName", "Ignat"); Console.WriteLine(p.Name());
using StaticReflection.Annotions; namespace StaticReflectionDemo; [StaticReflection] internal partial class Person { public string FirstName { get; set; } public string LastName { get; set; } public string Name() { return $"{FirstName} {LastName}"; } }
The code that is generated is
// <auto-generated/> #pragma warning disable CS9082 #pragma warning disable CS8669 namespace StaticReflectionDemo { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("StaticReflection.CodeGen", "1.0.0")] [global::System.Diagnostics.DebuggerStepThrough] [global::System.Runtime.CompilerServices.CompilerGenerated] internal sealed class PersonReflection : StaticReflection.ITypeDefine { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("StaticReflection.CodeGen", "1.0.0")] [global::System.Diagnostics.DebuggerStepThrough] [global::System.Runtime.CompilerServices.CompilerGenerated] internal sealed class Person0PReflection : StaticReflection.IMemberInvokeDefine<StaticReflectionDemo.Person, string>, StaticReflection.IPropertyDefine, StaticReflection.IMemberAnonymousInvokeDefine { public static readonly Person0PReflection Instance = new Person0PReflection(); public System.Type DeclareType { get; } = typeof(StaticReflectionDemo.Person); public System.String Name { get; } = "FirstName"; public System.String MetadataName { get; } = "FirstName"; public System.Boolean IsVirtual { get; } = false; public System.Boolean IsStatic { get; } = false; public System.Boolean IsOverride { get; } = false; public System.Boolean IsAbstract { get; } = false; public System.Boolean IsSealed { get; } = false; public System.Boolean IsDefinition { get; } = true; public System.Boolean IsExtern { get; } = false; public System.Boolean IsImplicitlyDeclared { get; } = false; public System.Boolean CanBeReferencedByName { get; } = true; public System.Boolean IsPublic { get; } = true; public System.Boolean IsPrivate { get; } = false; public System.Boolean IsProtected { get; } = false; public System.Boolean IsInternal { get; } = false; public System.Type PropertyType { get; } = typeof(string); public System.Boolean CanRead { get; } = true; public System.Boolean CanWrite { get; } = true; public System.Boolean IsRequired { get; } = false; public System.Boolean IsWithEvents { get; } = false; public System.Boolean ReturnsByRef { get; } = false; public System.Boolean ReturnsByRefReadonly { get; } = false; public System.Collections.Generic.IReadOnlyList<System.Attribute> GetterAttributes { get; } = new System.Attribute[] { }; public System.Collections.Generic.IReadOnlyList<System.Attribute> SetterAttributes { get; } = new System.Attribute[] { }; public System.Collections.Generic.IReadOnlyList<System.Attribute> Attributes { get; } = new System.Attribute[] { }; [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public string GetValue(Person instance) { return instance.FirstName; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public void SetValue(Person instance, string value) { instance.FirstName = value; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public void SetValueAnonymous(object instance, object value) { SetValue((StaticReflectionDemo.Person)instance, (string)value); } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public object GetValueAnonymous(object instance) { return (object)GetValue((StaticReflectionDemo.Person)instance); } } [global::System.CodeDom.Compiler.GeneratedCodeAttribute("StaticReflection.CodeGen", "1.0.0")] [global::System.Diagnostics.DebuggerStepThrough] [global::System.Runtime.CompilerServices.CompilerGenerated] internal sealed class Person1PReflection : StaticReflection.IMemberInvokeDefine<StaticReflectionDemo.Person, string>, StaticReflection.IPropertyDefine, StaticReflection.IMemberAnonymousInvokeDefine { public static readonly Person1PReflection Instance = new Person1PReflection(); public System.Type DeclareType { get; } = typeof(StaticReflectionDemo.Person); public System.String Name { get; } = "LastName"; public System.String MetadataName { get; } = "LastName"; public System.Boolean IsVirtual { get; } = false; public System.Boolean IsStatic { get; } = false; public System.Boolean IsOverride { get; } = false; public System.Boolean IsAbstract { get; } = false; public System.Boolean IsSealed { get; } = false; public System.Boolean IsDefinition { get; } = true; public System.Boolean IsExtern { get; } = false; public System.Boolean IsImplicitlyDeclared { get; } = false; public System.Boolean CanBeReferencedByName { get; } = true; public System.Boolean IsPublic { get; } = true; public System.Boolean IsPrivate { get; } = false; public System.Boolean IsProtected { get; } = false; public System.Boolean IsInternal { get; } = false; public System.Type PropertyType { get; } = typeof(string); public System.Boolean CanRead { get; } = true; public System.Boolean CanWrite { get; } = true; public System.Boolean IsRequired { get; } = false; public System.Boolean IsWithEvents { get; } = false; public System.Boolean ReturnsByRef { get; } = false; public System.Boolean ReturnsByRefReadonly { get; } = false; public System.Collections.Generic.IReadOnlyList<System.Attribute> GetterAttributes { get; } = new System.Attribute[] { }; public System.Collections.Generic.IReadOnlyList<System.Attribute> SetterAttributes { get; } = new System.Attribute[] { }; public System.Collections.Generic.IReadOnlyList<System.Attribute> Attributes { get; } = new System.Attribute[] { }; [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public string GetValue(Person instance) { return instance.LastName; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public void SetValue(Person instance, string value) { instance.LastName = value; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public void SetValueAnonymous(object instance, object value) { SetValue((StaticReflectionDemo.Person)instance, (string)value); } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public object GetValueAnonymous(object instance) { return (object)GetValue((StaticReflectionDemo.Person)instance); } } [global::System.CodeDom.Compiler.GeneratedCodeAttribute("StaticReflection.CodeGen", "1.0.0")] [global::System.Diagnostics.DebuggerStepThrough] [global::System.Runtime.CompilerServices.CompilerGenerated] internal sealed class Person0MReflection : StaticReflection.IMethodDefine, StaticReflection.Invoking.IArgsMethod<StaticReflectionDemo.Person, string>, StaticReflection.Invoking.IArgs0AnonymousMethod, StaticReflection.Invoking.IUsualArgsMethod<StaticReflectionDemo.Person, string>, StaticReflection.Invoking.IUsualArgs0AnonymousMethod { public static readonly Person0MReflection Instance = new Person0MReflection(); private Person0MReflection() { } public System.String Name { get; } = "Name"; public System.String MetadataName { get; } = "Name"; public System.Boolean IsVirtual { get; } = false; public System.Boolean IsStatic { get; } = false; public System.Boolean IsOverride { get; } = false; public System.Boolean IsAbstract { get; } = false; public System.Boolean IsSealed { get; } = false; public System.Boolean IsDefinition { get; } = true; public System.Boolean IsExtern { get; } = false; public System.Boolean IsImplicitlyDeclared { get; } = false; public System.Boolean CanBeReferencedByName { get; } = true; public System.Boolean IsPublic { get; } = true; public System.Boolean IsPrivate { get; } = false; public System.Boolean IsProtected { get; } = false; public System.Boolean IsInternal { get; } = false; public System.Collections.Generic.IReadOnlyList<System.Attribute> Attributes { get; } = new System.Attribute[] { }; public System.Type DeclareType { get; } = typeof(StaticReflectionDemo.Person); public System.Boolean ReturnsByRef { get; } = false; public StaticReflection.StaticMethodKind MethodKind { get; } = StaticReflection.StaticMethodKind.Ordinary; public StaticReflection.StaticRefKind RefKind { get; } = StaticReflection.StaticRefKind.None; public StaticReflection.StaticNullableAnnotation ReturnNullableAnnotation { get; } = StaticReflection.StaticNullableAnnotation.NotAnnotated; public StaticReflection.StaticNullableAnnotation ReceiverNullableAnnotation { get; } = StaticReflection.StaticNullableAnnotation.NotAnnotated; public System.Boolean ReturnsByRefReadonly { get; } = false; public System.Type ReturnType { get; } = typeof(string); public System.Collections.Generic.IReadOnlyList<System.Type> ArgumentTypes { get; } = new System.Type[] { }; public System.Boolean IsGenericMethod { get; } = false; public System.Int32 Arity { get; } = 0; public System.Boolean IsExtensionMethod { get; } = false; public System.Boolean IsAsync { get; } = false; public System.Boolean IsVararg { get; } = false; public System.Boolean IsCheckedBuiltin { get; } = false; public System.Boolean HidesBaseMethodsByName { get; } = false; public System.Boolean ReturnsVoid { get; } = false; public System.Boolean IsReadOnly { get; } = false; public System.Boolean IsInitOnly { get; } = false; public System.Boolean IsPartialDefinition { get; } = false; public System.Boolean IsConditional { get; } = false; public System.Collections.Generic.IReadOnlyList<StaticReflection.ITypeArgumentDefine> TypeArguments { get; } = new StaticReflection.ITypeArgumentDefine[] { }; public System.Collections.Generic.IReadOnlyList<System.Attribute> ReturnTypeAttributes { get; } = new System.Attribute[] { }; [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public #if !NET7_0_OR_GREATER unsafe #endif ref string Invoke(StaticReflectionDemo.Person instance) { ref string result = ref System.Runtime.CompilerServices.Unsafe.AsRef(instance.Name()); return ref result; } public #if !NET7_0_OR_GREATER unsafe #endif ref object InvokeAnonymous(object instance) { return ref System.Runtime.CompilerServices.Unsafe.AsRef<object>(Invoke((StaticReflectionDemo.Person)instance)); } [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public string InvokeUsual(StaticReflectionDemo.Person instance) { return instance.Name(); } public object InvokeUsualAnonymous(object instance) { return InvokeUsual((StaticReflectionDemo.Person)instance); } } [global::System.CodeDom.Compiler.GeneratedCodeAttribute("StaticReflection.CodeGen", "1.0.0")] [global::System.Diagnostics.DebuggerStepThrough] [global::System.Runtime.CompilerServices.CompilerGenerated] internal sealed class Person0CReflection : StaticReflection.IConstructorDefine, StaticReflection.Invoking.IArgsMethod<StaticReflectionDemo.Person, StaticReflectionDemo.Person>, StaticReflection.Invoking.IArgs0AnonymousMethod, StaticReflection.Invoking.IUsualArgsMethod<StaticReflectionDemo.Person, StaticReflectionDemo.Person>, StaticReflection.Invoking.IUsualArgs0AnonymousMethod { public static readonly Person0CReflection Instance = new Person0CReflection(); private Person0CReflection() { } public System.String Name { get; } = ".ctor"; public System.String MetadataName { get; } = ".ctor"; public System.Boolean IsVirtual { get; } = false; public System.Boolean IsStatic { get; } = false; public System.Boolean IsOverride { get; } = false; public System.Boolean IsAbstract { get; } = false; public System.Boolean IsSealed { get; } = false; public System.Boolean IsDefinition { get; } = true; public System.Boolean IsExtern { get; } = false; public System.Boolean IsImplicitlyDeclared { get; } = true; public System.Boolean CanBeReferencedByName { get; } = false; public System.Boolean IsPublic { get; } = true; public System.Boolean IsPrivate { get; } = false; public System.Boolean IsProtected { get; } = false; public System.Boolean IsInternal { get; } = false; public System.Collections.Generic.IReadOnlyList<System.Attribute> Attributes { get; } = new System.Attribute[] { }; public System.Type DeclareType { get; } = typeof(StaticReflectionDemo.Person); public System.Boolean ReturnsByRef { get; } = false; public StaticReflection.StaticMethodKind MethodKind { get; } = StaticReflection.StaticMethodKind.Constructor; public StaticReflection.StaticRefKind RefKind { get; } = StaticReflection.StaticRefKind.None; public StaticReflection.StaticNullableAnnotation ReturnNullableAnnotation { get; } = StaticReflection.StaticNullableAnnotation.NotAnnotated; public StaticReflection.StaticNullableAnnotation ReceiverNullableAnnotation { get; } = StaticReflection.StaticNullableAnnotation.NotAnnotated; public System.Boolean ReturnsByRefReadonly { get; } = false; public System.Type ReturnType { get; } = typeof(StaticReflectionDemo.Person); public System.Collections.Generic.IReadOnlyList<System.Type> ArgumentTypes { get; } = new System.Type[] { }; public System.Boolean IsGenericMethod { get; } = false; public System.Int32 Arity { get; } = 0; public System.Boolean IsExtensionMethod { get; } = false; public System.Boolean IsAsync { get; } = false; public System.Boolean IsVararg { get; } = false; public System.Boolean IsCheckedBuiltin { get; } = false; public System.Boolean HidesBaseMethodsByName { get; } = false; public System.Boolean ReturnsVoid { get; } = true; public System.Boolean IsReadOnly { get; } = false; public System.Boolean IsInitOnly { get; } = false; public System.Boolean IsPartialDefinition { get; } = false; public System.Boolean IsConditional { get; } = false; public System.Collections.Generic.IReadOnlyList<StaticReflection.ITypeArgumentDefine> TypeArguments { get; } = new StaticReflection.ITypeArgumentDefine[] { }; public System.Collections.Generic.IReadOnlyList<System.Attribute> ReturnTypeAttributes { get; } = new System.Attribute[] { }; [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public #if !NET7_0_OR_GREATER unsafe #endif ref StaticReflectionDemo.Person Invoke(StaticReflectionDemo.Person instance) { ref StaticReflectionDemo.Person result = ref System.Runtime.CompilerServices.Unsafe.AsRef(new Person()); return ref result; } public #if !NET7_0_OR_GREATER unsafe #endif ref object InvokeAnonymous(object instance) { return ref System.Runtime.CompilerServices.Unsafe.AsRef<object>(Invoke((StaticReflectionDemo.Person)instance)); } [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public StaticReflectionDemo.Person InvokeUsual(StaticReflectionDemo.Person instance) { return new Person(); } public object InvokeUsualAnonymous(object instance) { return InvokeUsual((StaticReflectionDemo.Person)instance); } } public static readonly PersonReflection Instance = new PersonReflection(); public System.Type DeclareType { get; } = typeof(StaticReflectionDemo.Person); public System.String Name { get; } = "Person"; public System.String MetadataName { get; } = "Person"; public System.Boolean IsVirtual { get; } = false; public System.Boolean IsStatic { get; } = false; public System.Boolean IsOverride { get; } = false; public System.Boolean IsAbstract { get; } = false; public System.Boolean IsSealed { get; } = false; public System.Boolean IsDefinition { get; } = true; public System.Boolean IsExtern { get; } = false; public System.Boolean IsImplicitlyDeclared { get; } = false; public System.Boolean CanBeReferencedByName { get; } = true; public System.Boolean IsPublic { get; } = false; public System.Boolean IsPrivate { get; } = false; public System.Boolean IsProtected { get; } = false; public System.Boolean IsInternal { get; } = true; public System.Type? BaseType { get; } = typeof(StaticReflectionDemo.Person); public System.Boolean IsReferenceType { get; } = true; public System.Boolean IsValueType { get; } = false; public System.Boolean IsAnonymousType { get; } = false; public System.Boolean IsTupleType { get; } = false; public System.Boolean IsNativeIntegerType { get; } = false; public System.Boolean IsRefLikeType { get; } = false; public System.Boolean IsUnmanagedType { get; } = false; public System.Boolean IsReadOnly { get; } = false; public System.Boolean IsRecord { get; } = false; public System.Int32 TypeKind { get; } = 2; public StaticReflection.StaticNullableAnnotation NullableAnnotation { get; } = StaticReflection.StaticNullableAnnotation.None; public System.Collections.Generic.IReadOnlyList<System.String> Interfaces { get; } = new System.String[] { }; public System.Collections.Generic.IReadOnlyList<System.String> AllInterfaces { get; } = new System.String[] { }; public System.Collections.Generic.IReadOnlyList<System.Attribute> Attributes { get; } = new System.Attribute[] { new StaticReflection.Annotions.StaticReflectionAttribute() { } }; public System.Collections.Generic.IReadOnlyList<StaticReflection.IPropertyDefine> Properties { get; } = new StaticReflection.IPropertyDefine[] { Person0PReflection.Instance, Person1PReflection.Instance }; public System.Collections.Generic.IReadOnlyList<StaticReflection.IMethodDefine> Methods { get; } = new StaticReflection.IMethodDefine[] { Person0MReflection.Instance }; public System.Collections.Generic.IReadOnlyList<StaticReflection.IEventDefine> Events { get; } = new StaticReflection.IEventDefine[] { }; public System.Collections.Generic.IReadOnlyList<StaticReflection.IFieldDefine> Fields { get; } = new StaticReflection.IFieldDefine[] { }; public System.Collections.Generic.IReadOnlyList<StaticReflection.IConstructorDefine> Constructors { get; } = new StaticReflection.IConstructorDefine[] { Person0CReflection.Instance }; } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/StaticReflection