c# - Creating Func body dynamically -


see sample below:

var factorytype = typeof(func<>).makegenerictype(sometype);  container.registerperrequest(sometype, null, sometype);  func<object> factorydelegate = () => container.getinstance(sometype, null); //this returns object, hence delegate type func<object>, required type func<someclass>  container.registerinstance(factorytype, null, factorydelegate); //not sure how create factory delegate 

i want create function body instance of sometype created using di container.

the idea configure di container can inject func<someclass> other classes.

how can done?

just cast result:

func<sometype> factorydelegate = () => container.getinstance(typeof(sometype), null) sometype; 

or create method (extension better):

static func<t> resolvetypedelegate<t>() t : class {     return () => resolvetype<t>(); }  static t resolvetype<t>() t : class {     return container.getinstance(typeof(sometype), null) t; } 

usage:

func<sometype> factorydelegate = resolvetypedelegate<sometype>(); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -