Skip to content

Fasjeit/DynamicReflector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DynamicReflector

DynamicReflector is a C# library that provides an easy way to access fields and methods on objects and static classes using dynamic and reflection.

Powered by Traverse from Harmony by Andreas Pardeike.

How it works

The Reflector object binds to another object and uses reflection to access its fields and methods.

When you access a member of the Reflector dynamic object, a new Reflector object is returned that is bound to the resulting object.

dynamic reflector = obj.ToReflector();
dynamic fieldReflection = reflector.fieled;
dynamic methodReflection =  reflector.method();

When you write to a member of the Reflector dynamic object, the value is written directly to the bound object.

dynamic reflector = obj.ToReflector();
reflector.fieled = 7; // changes binded object fieled value

You can always get original object from reflector

dynamic reflector = obj.ToReflector();
dynamic fieldReflection = reflector.fieled;
object field = fieldReflection.OriginalObject;

Installation

Install nuget packet

via dotnet

dotnet add package DynamicReflector

via nuget

Install-Package DynamicReflector

Usage

// Getting reflector for an object
object obj = new InternalClass();
dynamic reflector = obj.ToReflector();

// ... or using new()
dynamic reflectorNew = new Reflector(obj);

// ... or using Create()
dynamic reflectorCreate = Reflector.Create(typeof(InternalClass), 1337);

// Getting private field value
// We need to explisitly cast OriginalObject to desired type
var value = (int)reflector.value.OriginalObject;

// Setting private field value
reflector.value = 7; // changes obj.value

// Call a method and get the result
var result = (int)reflector.ReturnValue().OriginalObject;

// Creating reflector for a class to call static methods
dynamic staticReflector = Reflector.CreateStatic(typeof(InternalClass));

// Call static method
var staticMethodResult = staticReflector.StaticMethod().OriginalObject;

Where InternalClass is:

internal class InternalClass
{
    private int value;

    private static string StaticMethod()
    {
        return "static";
    }

    public InternalClass() 
        : this(0)
    {
    }

    private InternalClass(int value)
    {
        this.value = value;
    }

    private int ReturnValue()
    {
        return this.value;
    }
}

About

A dotnet library that provides an easy way to access fields and methods on objects and static classes using dynamic and reflection.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages