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
Post a Comment