Last active
August 29, 2015 14:04
-
-
Save philiplaureano/956e6a3d70ba09362a26 to your computer and use it in GitHub Desktop.
An example of how to use Functify to call and instantiate a generic method at runtime.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FunctifyBench | |
{ | |
using System.Reflection; | |
using Functify; | |
public class FakeContainer | |
{ | |
public void RegisterType<TInterface, TImplementation>() | |
{ | |
Console.WriteLine("Registering type {0} as {1}", typeof(TInterface), typeof(TImplementation)); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Let's assume that this container | |
// has a registration method called RegisterType<T>() | |
var container = new FakeContainer(); | |
// Use the GenericCallTo().WithTypeArguments(...).WithArguments(...) method chain to create an Action<Type,Type> that | |
// registers the service instance | |
Action<Type, Type> registerType = (interfaceType, implementingType) => container.GenericCallTo("RegisterType") | |
.WithTypeArguments(interfaceType, implementingType).WithArguments(); | |
// Call the action, and register the service | |
registerType(typeof(int), typeof(int)); | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment