Create object at runtime.. is reflection needed?

  • Thread starter Thread starter Jay Allard
  • Start date Start date
J

Jay Allard

Hello

I'm sure this is already posted somewhere, I'm just not using the
correct search words or something.

Anyway, I want to store a class name in a database:
TestApplication.Validators.MyCustomClass
(MyCustomClass is the class name).

MyCustomClass implements an interface named IValidator. I know that
every class specified in the database will implement that interface.
Knowing that, is it possible to dynamically create the object, or do I
have to use reflection?

I'm looking for something like

IValidator v = something.something("TestApplication.Validators.MyCustomClass");
so I can then use the methods defined in the IValidator interface.

v.SetAProperty("whatever");
v.Validate();

Is that possible, or do I have to use reflection and InvokeMember for
each method?

Thanks

Jay
 
Perfect. Thanks.

In case anyone else comes across this, here's the actual code I used.

System.Runtime.Remoting.ObjectHandle oh =
Activator.CreateInstance(sValidationAssembly, sValidationClass);
WhatTypeOfObjectItIs x = (WhatTypeOfObjectItIs)oh.Unwrap();
 
Back
Top