DynamicBinder 1.5.1

This library allows you to dynamic access to object methods, properties, and fields even if they are private members by Reflection technology.
You can access the both of object instance members and class static members by name that specified string argument at runtime not compile time, or C# 4.0 "dynamic" syntax.

Install-Package DynamicBinder -Version 1.5.1
dotnet add package DynamicBinder --version 1.5.1
<PackageReference Include="DynamicBinder" Version="1.5.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DynamicBinder --version 1.5.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

DynamicBinder and LateBinder NuGet Package

What's this?

This is the class library for .NET.

This library allows you to dynamic access to object methods, properties, and fields
even if they are private members by Reflection technology.

You can access the both of object instance members and class static members by name that specified string argument at runtime not compile time, or C# 4.0 "dynamic" syntax.

How to install?

You can install this library via NuGet.

PM> Install-Package DynamicBinder

How to use?

C# "dynamic" syntax

Instance member access

After import(open) namespace "Toolbelt.DynamicBinderExtension",
you can use ToDynamic() extension method that returned
C#4.0 "dynamic" type at any object.

using Toolbelt.DynamicBinderExtension;
...
// call instance method.
var retval = (int)obj.ToDynamic().MethodName(arg1, arg2);

// get & set instance property.
var value = (int)obj.ToDynamic().PropName;
obj.ToDynamic().PropName = newValue;

// get & set instance field.
var value = (int)obj.ToDynamic().FieldName;
obj.ToDynamic().FieldName = newValue;
Static member access

After import(open) namespace "Toolbelt",
you can use DynamicBinder.Create&lt;T&gt;() and
DynamicBinder.Create(Type t) static method that returned
C#4.0 "dynamic" type.

using Toolbelt;
...
var binder = DynamicBinder.Create(typeof(Foo));
// call static method.
var retval = (int)binder.MethodName(arg1, arg2);

// get & set static property.
var value = (int)binder.PropName;
binder.PropName = newValue;

// get & set static field.
var value = (int)binder.FieldName;
binder.FieldName = newValue;

Notice: retrieve class type return value from method calling

the follow test code is fail.

object bar = foo.ToDynamic().GetBarObject();
Assert.AreEqual("BarClass", bar.GetType().Name); // report actual is "DynamicBinder"!

Because C# dynamic conversion type to object is return DynamicBinder object it self.

You should rewrite avobe test code as follow.

// etract C# dynamic object to DynamicBinder object, static type world.
var retval = foo.ToDynamic().GetBarObject() as DynamicBinder;
// DynamicBinder class exposed "Object" property to access the binding target object.
Assert.AreEqual("BarClass", retval.Object.GetType().Name); // Green. Pass!

Of course, if you have the right type information, thease test codes can write follow:

var bar = (BarClass)foo.ToDynamic().GetBarObject();
Assert.AreEqual("BarClass", bar.GetType().Name); // Green. Pass!

Late bind syntax

Instance member access

After import(open) namespace "Toolbelt.DynamicBinderExtension",
you can use ToLateBind() extension method that returned
"LateBinder" object at any object.

"LateBinder" has follow members.


  • Call(name, paams[] args) method
  • Prop[name] property
  • Field[name] property

using Toolbelt.DynamicBinderExtension;
...
// call method.
var retval = (int)obj.ToLateBind().Call("MethodName", arg1, arg2);

// get & set property.
var value = (int)obj.ToLateBind().Prop["PropName"];
obj.ToLateBind().Prop["PropName"] = newValue;

// get & set field.
var value = (int)obj.ToLateBind().Field["FieldName"];
obj.ToLateBind().Field["FieldName"] = newValue;
Static member access

After import(open) namespace "Toolbelt",
you can use LateBinder.Create&lt;T&gt;() and
LateBinder.Create(Type t) static method that returned
"LateBinder" object.

using Toolbelt;
...
var binder = LateBinder.Create<Foo>();
// call static method.
var retval = (int)binder.Call("MethodName", arg1, arg2);

// get & set static property.
var value = (int)binder.Prop["PropName"];
binder.Prop["PropName"] = newValue;

// get & set static field.
var value = (int)binder.Field["FieldName"];
binder.Field["FieldName"] = newValue;

No use extension methods

If you feel these extension method is dirty, you can chose no using these extension method.

Instead, you can use LateBinder class and DynamicBinder class like follow code.

// do not open namespace "Toolbelt.DynamicBinderExtension".
using Toolbelt;
...
dynamic dynamicBinder = new DynamicBinder(obj);
var retval = (int)dynamicBinder.MethodName(arg1, arg2);

var lateBinder = new LateBinder(obj);
var retval = (int)lateBinder.Call("MethodName", arg1, arg2);

Note

"reinventing the wheel"

There are many many packages more than 50 about reflection & private member accessing library.

https://www.nuget.org/packages?q=Tags%3A%22reflection%22

But I couldn't find any library which has my favorit syntax and features :).

So I deside to "reinvent the wheel".

Performance issue

This library "DynamicBinder" and "LateBinder" may be very slowly because the implementation of
this library is calling reflection API directory without chache, compile to delegate, complile to expression,
and so on.

There is plenty room for improvement to more faster, more high performance.

If you prefer, you can fork this source codes and improve it.

DynamicBinder and LateBinder NuGet Package

What's this?

This is the class library for .NET.

This library allows you to dynamic access to object methods, properties, and fields
even if they are private members by Reflection technology.

You can access the both of object instance members and class static members by name that specified string argument at runtime not compile time, or C# 4.0 "dynamic" syntax.

How to install?

You can install this library via NuGet.

PM> Install-Package DynamicBinder

How to use?

C# "dynamic" syntax

Instance member access

After import(open) namespace "Toolbelt.DynamicBinderExtension",
you can use ToDynamic() extension method that returned
C#4.0 "dynamic" type at any object.

using Toolbelt.DynamicBinderExtension;
...
// call instance method.
var retval = (int)obj.ToDynamic().MethodName(arg1, arg2);

// get & set instance property.
var value = (int)obj.ToDynamic().PropName;
obj.ToDynamic().PropName = newValue;

// get & set instance field.
var value = (int)obj.ToDynamic().FieldName;
obj.ToDynamic().FieldName = newValue;
Static member access

After import(open) namespace "Toolbelt",
you can use DynamicBinder.Create&lt;T&gt;() and
DynamicBinder.Create(Type t) static method that returned
C#4.0 "dynamic" type.

using Toolbelt;
...
var binder = DynamicBinder.Create(typeof(Foo));
// call static method.
var retval = (int)binder.MethodName(arg1, arg2);

// get & set static property.
var value = (int)binder.PropName;
binder.PropName = newValue;

// get & set static field.
var value = (int)binder.FieldName;
binder.FieldName = newValue;

Notice: retrieve class type return value from method calling

the follow test code is fail.

object bar = foo.ToDynamic().GetBarObject();
Assert.AreEqual("BarClass", bar.GetType().Name); // report actual is "DynamicBinder"!

Because C# dynamic conversion type to object is return DynamicBinder object it self.

You should rewrite avobe test code as follow.

// etract C# dynamic object to DynamicBinder object, static type world.
var retval = foo.ToDynamic().GetBarObject() as DynamicBinder;
// DynamicBinder class exposed "Object" property to access the binding target object.
Assert.AreEqual("BarClass", retval.Object.GetType().Name); // Green. Pass!

Of course, if you have the right type information, thease test codes can write follow:

var bar = (BarClass)foo.ToDynamic().GetBarObject();
Assert.AreEqual("BarClass", bar.GetType().Name); // Green. Pass!

Late bind syntax

Instance member access

After import(open) namespace "Toolbelt.DynamicBinderExtension",
you can use ToLateBind() extension method that returned
"LateBinder" object at any object.

"LateBinder" has follow members.


  • Call(name, paams[] args) method
  • Prop[name] property
  • Field[name] property

using Toolbelt.DynamicBinderExtension;
...
// call method.
var retval = (int)obj.ToLateBind().Call("MethodName", arg1, arg2);

// get & set property.
var value = (int)obj.ToLateBind().Prop["PropName"];
obj.ToLateBind().Prop["PropName"] = newValue;

// get & set field.
var value = (int)obj.ToLateBind().Field["FieldName"];
obj.ToLateBind().Field["FieldName"] = newValue;
Static member access

After import(open) namespace "Toolbelt",
you can use LateBinder.Create&lt;T&gt;() and
LateBinder.Create(Type t) static method that returned
"LateBinder" object.

using Toolbelt;
...
var binder = LateBinder.Create<Foo>();
// call static method.
var retval = (int)binder.Call("MethodName", arg1, arg2);

// get & set static property.
var value = (int)binder.Prop["PropName"];
binder.Prop["PropName"] = newValue;

// get & set static field.
var value = (int)binder.Field["FieldName"];
binder.Field["FieldName"] = newValue;

No use extension methods

If you feel these extension method is dirty, you can chose no using these extension method.

Instead, you can use LateBinder class and DynamicBinder class like follow code.

// do not open namespace "Toolbelt.DynamicBinderExtension".
using Toolbelt;
...
dynamic dynamicBinder = new DynamicBinder(obj);
var retval = (int)dynamicBinder.MethodName(arg1, arg2);

var lateBinder = new LateBinder(obj);
var retval = (int)lateBinder.Call("MethodName", arg1, arg2);

Note

"reinventing the wheel"

There are many many packages more than 50 about reflection & private member accessing library.

https://www.nuget.org/packages?q=Tags%3A%22reflection%22

But I couldn't find any library which has my favorit syntax and features :).

So I deside to "reinvent the wheel".

Performance issue

This library "DynamicBinder" and "LateBinder" may be very slowly because the implementation of
this library is calling reflection API directory without chache, compile to delegate, complile to expression,
and so on.

There is plenty room for improvement to more faster, more high performance.

If you prefer, you can fork this source codes and improve it.

Release Notes

v.1.5.1
- Enhance: Add .NET Standard 2.0 support.
v.1.5
- Enhance: Add "Object" property which expose the object that binding target.
- Fix bug: Can not extract class type object by DynamicBinder.
v.1.4
- Fix bug: Can not retrieve class type properties by DynamicBinder.
v.1.3
- Add avility of exposing nested private object graph.
v.1.2
- Add avility of caching System.Reflection.MemberInfo (use SetCache(disctionary) method)
- Add GetInfo(name) method on PropertyBinder and FieldBinder that returned System.Reflection.MemberInfo.
v.1.1
- Support base class members access.

  • .NETFramework 4.0

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.5.1 660 12/22/2017
1.5.0 1,646 4/22/2014
1.4.0 505 4/18/2014
1.3.0 508 3/14/2014
1.2.0 514 3/12/2014
1.1.0 512 3/1/2014
1.0.0 510 2/22/2014