I have function as below .
public static object getClassInstance(string key, object constructorParameter)
{
// body here
}
Key variable will have my class name . I need to return the new instance of the class . If the constructorParm is null then i need to load the class with default constructor else with the constructor parameter passed. How do i do this ?
ADD :
I wrote the code like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Catalyst.BO.StudentProfileBO;
using Catalyst.BO.ReportBO;
using Catalyst.DAL.ReportDAO;
using System.Collections;
using System.Data;
namespace Catalyst.BO.Factory
{
public class CFactory
{
public static object getClassInstance(string key, params object[] constructorArgs)
{
string assemblyPath = null;
string customClassName = key.Substring(0, 1) + "Custom" + key.Substring(1);
DataSet objDataset = getAssemblyInfo(key);
if (objDataset != null && objDataset.Tables.Count > 0 && objDataset.Tables[0].Rows.Count > 0)
{
assemblyPath = objDataset.Tables[0].Rows[0]["ACA_ASSEMBLY_PATH"].ToString();
}
Assembly assembly;
Type type;
if (assemblyPath != null && assemblyPath != string.Empty)
{
assembly = Assembly.LoadFile(assemblyPath);
type = assembly.GetType(customClassName);
}
else // if no customisation
{
type = Type.GetType(key);
}
object classInstance = constructorArgs == null ? Activator.CreateInstance(type) : Activator.CreateInstance(type, constructorArgs);
if (classInstance == null) throw new Exception("broke");
return classInstance;
}
}
}
key passed to the function is "CReportBO". CReportBO is accessible in the function's scope . but in //if no customization section (i.e type = Type.GetType(key) ) , type returns me null . whats wrong ?
nullas the parameter ;p