Reflection and Constructors

  • Thread starter Thread starter V. Jenks
  • Start date Start date
V

V. Jenks

I am using reflection to load classes and I can't find any
examples on how to pass values to constructor parameters
using reflection, how is this done?

Here's an example of one of my methods, as you can see it
accepts two parameters. I want to pass those two values to
the constructor of the class I'm loading via reflection.

public IDbAdapter CreateAdapter(string query,
IDbConnection onnection)
{
string dbType = DB_ASSEMBLY;
string dbAdapterClass = Config.DbAdapterClass;

//load the assembly and class
IDbAdapter da =
(IDbAdapter)Assembly.Load(assemblyName).CreateInstance(className);

return da;
}

Thanks in advance!
 
V. Jenks said:
I am using reflection to load classes and I can't find any
examples on how to pass values to constructor parameters
using reflection, how is this done?

Either use Activator.CreateInstance (Type, object[]) or from the Type
reference, find the constructor you want (using Type.GetConstructor)
and then call Invoke on it.
 
Back
Top