cmstar.RapidReflection 1.0.0

dotnet add package cmstar.RapidReflection --version 1.0.0                
NuGet\Install-Package cmstar.RapidReflection -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="cmstar.RapidReflection" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add cmstar.RapidReflection --version 1.0.0                
#r "nuget: cmstar.RapidReflection, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install cmstar.RapidReflection as a Cake Addin
#addin nuget:?package=cmstar.RapidReflection&version=1.0.0

// Install cmstar.RapidReflection as a Cake Tool
#tool nuget:?package=cmstar.RapidReflection&version=1.0.0                

RapidReflection

Simply accesses type memebers with dynamic methods created using Reflection.Emit.

Supported .net versions:

  • .net Framework 3.5
  • .net Framework 4.x
  • Other versions that are compliant with .net Standard 2.0, such as .net Core 2/3, .net 5/6

Usage

The class for test

class TestClass
{
    private int _integer;

    public TestClass() { }
    public TestClass(int integer) { _integer = integer; }

    public int Integer { get { return _integer; } set { _integer = value; } }

    public string GetHello(string target)
    {
        return string.Format("Hello {0}!", target); 
    }
}

Property Access

PropertyInfo property = typeof(TestClass).GetProperty("Integer");
TestClass instance = new TestClass { Integer = 12345 };

Func<object, object> getter = PropertyAccessorGenerator.CreateGetter(property);
Console.WriteLine(getter(instance)); // -> 12345

Action<object, object> setter = PropertyAccessorGenerator.CreateSetter(property);
setter(instance, 54321);
Console.WriteLine(instance.Integer); // -> 54321

Field Access

FieldInfo field = typeof(TestClass).GetField(
    "_integer", BindingFlags.Instance | BindingFlags.NonPublic);
TestClass instance = new TestClass { Integer = 12345 };

Func<object, object> getter = FieldAccessorGenerator.CreateGetter(field);
Console.WriteLine(getter(instance)); // -> 12345

Action<object, object> setter = FieldAccessorGenerator.CreateSetter(field);
setter(instance, 54321);
Console.WriteLine(instance.Integer); // -> 54321

For static property/field access, use null as the first argument:

getter(null);
setter(null, value);

Constructor Invoking

ConstructorInfo constructor
    = typeof(TestClass).GetConstructor(new Type[] { typeof(int) });
Func<object[], object> func
    = ConstructorInvokerGenerator.CreateDelegate(constructor);

object[] arguments = new object[] { 123 };
TestClass instance = (TestClass)func(arguments);
Console.WriteLine(instance.Integer); // -> 123

or use the parameterless constructor directly:

Func<object> func 
    = ConstructorInvokerGenerator.CreateDelegate(typeof(TestClass));
TestClass instance = (TestClass)func();

Method Invoking

MethodInfo method = typeof(TestClass).GetMethod("GetHello");
Func<object, object[], object> caller
    = MethodInvokerGenerator.CreateDelegate(method);

TestClass instance = new TestClass();
object[] arguments = new object[] { "World" };
string result = (string)caller(instance, arguments);
Console.WriteLine(result); // -> "Hello World!"

To call a static method, use null as the instance:

caller(null, arguments);

If the method has no argument, either is OK:

caller(instanc, null);
caller(instanc, new object[0]);

If the method has no return value, the delegate returns null.

Extention Methods

A set of extention methods is provided so the code below

ILGenerator il = dynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldlen);
il.Emit(OpCodes.Conv_I4);

can be written in this form:

ILGenerator il = dynamicMethod.GetILGenerator();
il.Ldarg_0().Ldlen().Conv_I4();

In the current version, the extention methods does not include operations for all opcodes.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net35 is compatible.  net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on cmstar.RapidReflection:

Package Downloads
cmstar.Data

A simple database client and object mapper.

cmstar.Serialization.Json

A light weight JSON serialization library.

cmstar.Caching

Provides cache abstraction and memory cache implementation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,033 5/4/2022