Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Instantiate class which implements generic interface
    text
    copied!<p>I have some business classes which implements <code>IBusinessRequest&lt;T&gt;</code>, for example:</p> <pre><code>public class PersonBusiness : IBusinessRequest&lt;Person&gt; { } </code></pre> <p>Besides this I have a function:</p> <pre><code> TypeHelper.CreateBusinessInstance(Type businessType, Type businessRequestType) </code></pre> <p>A requirement of a business class is that they must have a parameterless constructor, which I check in the <code>TypeHelper.CreateBusinessInstance</code> function.</p> <p>I want to create a instance of type businessType (which is <code>PersonBusiness</code>) with the generic value <code>businessRequestType</code> for <code>IBusinessRequest&lt;&gt;</code>.</p> <p>How can I get this done?</p> <p>EDIT1:</p> <p>Thanks for all answers it putted me on the right track. The situation I wrote down was not the real situation I was dealing with. I hoped it was just enough to solve my problem :-)</p> <p>I now came up with the following, which works like charm.</p> <pre><code>public interface IBusinessRequest&lt;T&gt; where T : class { T Request { get; set; } } public interface IBusiness { /// &lt;summary&gt; /// Validates the request against custom rules /// &lt;/summary&gt; /// &lt;param name="meldingen"&gt;Return a list of validation messages&lt;/param&gt; /// &lt;returns&gt;returns true is validation went succesfull, false when something is wrong&lt;/returns&gt; bool Validate(out List&lt;string&gt; meldingen); /// &lt;summary&gt; /// Executes business logic and returns a response object. /// &lt;/summary&gt; /// &lt;returns&gt;The strongly typed response object&lt;/returns&gt; object Execute(object request); } public class PersonBusiness :IBusiness, IBusinessRequest&lt;Person&gt; { } public static IBusiness CreateBusinessInstance(Type type, object requestMessage) { //some checks on the type, like: is of IBusiness &amp; IBusinessRequest&lt;&gt; ... //get al constructors of type ConstructorInfo[] constructors = type.GetConstructors(); //if we have more then one constructor throw error if (constructors.Length &gt; 1) { throw new BusinessCreateException(String.Format(PrivateErrors.ToManyConstructorsInTypeError, type, constructors.Length, 1)); } //constructor parameters ParameterInfo[] parameterInfo = constructors[0].GetParameters(); //we do not allow a constructor with more then one parameters. if (parameterInfo.Length &gt; 0) { throw new BusinessCreateException(String.Format(PrivateErrors.ConstructorHasToManyParametersError, type, 0)); } IBusiness instance = null; try { //create an instance, invoke constructor with zero parameters object invokeResult = constructors[0].Invoke(new object[0]); //set property "Request" PropertyInfo pi = type.GetProperty("Request"); //do we have found a property if (pi != null) { pi.SetValue(invokeResult, requestMessage ,null); } instance = invokeResult as IBusiness; } catch (Exception ex) { throw new BusinessCreateException(String.Format(PrivateErrors.BusinessCreateError, type.FullName), ex); } //return result return instance; } </code></pre> <p>Now at runtime in a other part of the sofware the business class type is given to me (i.e PersonBusiness). Another fact is the part that I know the requestMessage which is the same that is given to IBusinessRequest. I need this requestMessage to set the property Request of the PersonBusiness class (which is implemented from IRequestMessage)</p> <p>These two variables I give to static IBusiness CreateBusinessInstance(Type type, object requestMessage) which gives me back the IBusiness, which i further use to execute some business function.</p> <p>Thanks all!</p> <p>Gr</p> <p>Martijn</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload