LiteWare.ObjectInvokers 0.1.0

There is a newer version of this package available.
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                
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="LiteWare.ObjectInvokers" Version="0.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LiteWare.ObjectInvokers --version 0.1.0                
#r "nuget: LiteWare.ObjectInvokers, 0.1.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 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

Version Version

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 on private members.

Finally, the members can be dynamically invoked by member name:

int result = (int)objectInvoker.Invoke("MyFunction", 123)!;
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
0.2.0 438 3/25/2022
0.1.0 422 2/15/2022

Initial code commit