LiteWare.ObjectInvokers
0.1.0
See the version list below for details.
dotnet add package LiteWare.ObjectInvokers --version 0.1.0
NuGet\Install-Package LiteWare.ObjectInvokers -Version 0.1.0
<PackageReference Include="LiteWare.ObjectInvokers" Version="0.1.0" />
paket add LiteWare.ObjectInvokers --version 0.1.0
#r "nuget: LiteWare.ObjectInvokers, 0.1.0"
// Install LiteWare.ObjectInvokers as a Cake Addin #addin nuget:?package=LiteWare.ObjectInvokers&version=0.1.0 // Install LiteWare.ObjectInvokers as a Cake Tool #tool nuget:?package=LiteWare.ObjectInvokers&version=0.1.0
LiteWare.ObjectInvokers
Dynamically invoke methods and modify properties or fields of an object by referencing the member's name.
This is done via the ObjectInvoker.Invoke
method which accepts a member name and any generic types and/or parameters and redirects the call to a concrete object.
Depending on the underlying concrete object member, the invoke process can:
Invoke a method and return the result if the member is a method:
bool success = (bool)objectInvoker.Invoke("SetLightSwitchState", "SW012", LightSwitchState.On)!;
Get or set a property if the member is a property:
string id = (string)objectInvoker.Invoke("DeviceId")!; // Property getter objectInvoker.Invoke("DeviceDescription", "Livingroom light switch"); // Property setter
Get or set a field value if the member is a field:
int timeout = (int)objectInvoker.Invoke("Timeout")!; // Get field value objectInvoker.Invoke("Timeout", 1000); // Set field value
This library is useful in scenarios like RPC/RMI, where a remote endpoint wants to invoke a local method by specifying its name.
Installation
The library is available as a Nuget Package.
Usage
To dynamically invoke members by member name, an ObjectInvoker
needs to be created, usually by binding a contract type containing the members to invoke to an instance of the contract:
ObjectInvoker objectInvoker = ObjectInvoker.Bind<IMyService>(myService);
The contract type is not limited to only interfaces and can be of any type. However, it must have members qualified by the InvokableMemberAttribute
so that they can be invoked:
public interface IMyService
{
[InvokableMember]
int MyProperty { get; set; }
[InvokableMember]
void MyProcedure();
[InvokableMember("MyFunction")]
int SomeFunction(int arg);
}
InvokableMemberAttribute
can also be applied onprivate
members.
Finally, the members can be dynamically invoked by member name:
int result = (int)objectInvoker.Invoke("MyFunction", 123)!;
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial code commit