Small library that allows you to invoke the specified action when object is of particular type.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
GenericInvoker.Tests
GenericInvoker
.gitignore
GenericInvoker.sln
LICENSE
README.md

README.md

GenericInvoker

GenericInvoker is small PortableClassLibrary that allows you to invoke the specified action when object is of particular type.

Available via nuget.

Background

There are some question on Stackoverflow, where authors ask how to handle case, when we have object and take specified steps only when the object is of concrete, hardcoded type. There are some solutions proposed:

object obj = GetObject();
if (obj is MyClass)
{
  var casted = obj as MyClass;
  // actions
}

if (obj is OtherClass)
{
  var casted = obj as OtherClass;
  // actions
}

// and so on

or:

void MyAction(MyClass class)
{
}

void MyAction(OtherClass)
{
}

object obj = GetObject();
MyAction((dynamic)obj);

GenericInvoker combines both solution with Fluent API:

using GenericInvoker;

//...

object obj = GetObject();
obj.DetermineType().When((MyClass target) => { /* action */ })
                   .When((OtherClass target) => { /* action */ })
                   .Resolve();