I have some business classes which implements IBusinessRequest<T>
, for example:
public class PersonBusiness : IBusinessRequest<Person>
{ }
Besides this I have a function:
TypeHelper.CreateBusinessInstance(Type businessType, Type businessRequestType)
A requirement of a business class is that they must have a parameterless constructor, which I check in the TypeHelper.CreateBusinessInstance
function.
I want to create a instance of type businessType (which is PersonBusiness
) with the generic value businessRequestType
for IBusinessRequest<>
.
How can I get this done?
EDIT1:
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 :-)
I now came up with the following, which works like charm.
public interface IBusinessRequest<T> where T : class
{
T Request { get; set; }
}
public interface IBusiness
{
/// <summary>
/// Validates the request against custom rules
/// </summary>
/// <param name="meldingen">Return a list of validation messages</param>
/// <returns>returns true is validation went succesfull, false when something is wrong</returns>
bool Validate(out List<string> meldingen);
/// <summary>
/// Executes business logic and returns a response object.
/// </summary>
/// <returns>The strongly typed response object</returns>
object Execute(object request);
}
public class PersonBusiness :IBusiness, IBusinessRequest<Person>
{ }
public static IBusiness CreateBusinessInstance(Type type, object requestMessage)
{
//some checks on the type, like: is of IBusiness & IBusinessRequest<>
...
//get al constructors of type
ConstructorInfo[] constructors = type.GetConstructors();
//if we have more then one constructor throw error
if (constructors.Length > 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 > 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;
}
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)
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.
Thanks all!
Gr
Martijn