Here's what I did in my code initially ...
public System.Web.UI.W ebControls.Base Validator[] GetValidators() {
System.Web.UI.W ebControls.Base Validator[] validators = new
System.Web.UI.W ebControls.Base Validator[this.ValidatorD escriptors.Leng th];
for(int i=0; i<this.Validato rDescriptors.Le ngth; i++){
ValidatorDescri ptor validator = this.ValidatorD escriptors[i];
ObjectHandle objHndl = Activator.Creat eInstance("Syst em.Web",
validator.Class Name);
object instance = objHndl.Unwrap( );
foreach(Propert y prop in validator.Prope rties)
TypeInspector.S etPropertyValue (prop.Name, prop.Value, instance);
try{
validators[i] = (System.Web.UI. WebControls.Bas eValidator)inst ance;
}catch(Exceptio n e){
throw new InvalidCastExce ption("Cannot cast to IValidator for class
"+instance.GetT ype().ToString( )+" original : "+validator.Cla ssName);
}
}
return validators;
}
... this is executed inside a Web Appliation, so the System.Web assembly
*should* be available to the Activator .. however, I get an exception saying
that the ASP.NET runtime cannot load the System.Web assembly !!! (Weird ?)
So, I changed the code:
ObjectHandle objHndl = Activator.Creat eInstance("Syst em.Web",
validator.Class Name);
object instance = objHndl.Unwrap( );
... to use a class I'd created on my own ...
object instance =
DynamicClassLoa der.GetInstance ().Load(validat or.ClassName);
what the DynamicClassLoa der does, is locate the class name in it's
configuration file, read the asembly it belongs to from there, and then:
... in the initialization method ...
AssemblyDescrip tor ad = (AssemblyDescri ptor)lstAssembl ies[i];
try{
Assembly objAssembly = Assembly.LoadFr om(ad.CodeBase) ;
... . in the Load(strClassna me) method
return objAssembly.Cre ateInstance(str ClassName);
....
That code works ok, and returns an instance of the class I'm asking for, the
RequiredFieldVa lidator. However, when I cast it to a BaseValidator, I get
the InvalidCastExce ption ...
... and that's my problem right there. I get some weird exception in both
attempts, either using dynamic assembly loading, or not :(
Angel
O:]
"Jorge Matos" <Jo********@dis cussions.micros oft.com> wrote in message
news:7B******** *************** ***********@mic rosoft.com...
Loading System.Web.dll should not load another instance of the assembly,
once an assembly is loaded into memory, you cannot load additional instances of
it. Please post an example of what your code. I would also try to create
a simple test app to isolate the code you are having a problem with.
"Angelos Karantzalis" wrote:
Hi guys,
I'm trying to load a class instance dynamically, and then cast it to
it's base type so I can use it in my app.
More specifically, I'm dynamically instantiating a
System.Web.UI.W ebControls.Requ iredFieldValida tor and attempting to cast
it into an IValidator and/or a BaseValidator. Both casting attempts have
failed miserably, throwing an InvalidCastExce ption ... how can it be i
cannot cast to the base class of my instance ???
More specifically, I load the Assembly directly from the System.Web.dll,
and create the RequiredFieldVa lidator instance using .CreateInstance (string
strClassName) on the Assembly.
I get back a perfectly good object, for which I'm using reflection to
set some properties before I cast it to it's base type (BaseValidator) and
return it from a method to the caller. upon casting it to the base type,
I get the InvalidCastExce ption. This shouldn't be happening normally, and
i'm bangin' my head against a wall to find out what could be causing it ...
Could it be that the application uses a reference to the System.Web.dll,
and my own loader loads another instance of the assembly ? If this is it,
how could I work around it ?
Thanks a million,
Angel
O:]