Reflection / Casting Question

  • Thread starter Angelos Karantzalis
  • Start date
A

Angelos Karantzalis

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.WebControls.RequiredFieldValidator and attempting to cast it
into an IValidator and/or a BaseValidator. Both casting attempts have
failed miserably, throwing an InvalidCastException ... 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 RequiredFieldValidator 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 InvalidCastException. 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:]
 
G

Guest

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.
 
A

Angelos Karantzalis

Here's what I did in my code initially ...

public System.Web.UI.WebControls.BaseValidator[] GetValidators() {


System.Web.UI.WebControls.BaseValidator[] validators = new
System.Web.UI.WebControls.BaseValidator[this.ValidatorDescriptors.Length];


for(int i=0; i<this.ValidatorDescriptors.Length; i++){

ValidatorDescriptor validator = this.ValidatorDescriptors;

ObjectHandle objHndl = Activator.CreateInstance("System.Web",
validator.ClassName);

object instance = objHndl.Unwrap();

foreach(Property prop in validator.Properties)

TypeInspector.SetPropertyValue(prop.Name, prop.Value, instance);


try{

validators = (System.Web.UI.WebControls.BaseValidator)instance;

}catch(Exception e){

throw new InvalidCastException("Cannot cast to IValidator for class
"+instance.GetType().ToString()+" original : "+validator.ClassName);

}

}

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.CreateInstance("System.Web",
validator.ClassName);

object instance = objHndl.Unwrap();

... to use a class I'd created on my own ...

object instance =
DynamicClassLoader.GetInstance().Load(validator.ClassName);

what the DynamicClassLoader 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 ...

AssemblyDescriptor ad = (AssemblyDescriptor)lstAssemblies;

try{

Assembly objAssembly = Assembly.LoadFrom(ad.CodeBase);

... . in the Load(strClassname) method

return objAssembly.CreateInstance(strClassName);

....

That code works ok, and returns an instance of the class I'm asking for, the
RequiredFieldValidator. However, when I cast it to a BaseValidator, I get
the InvalidCastException ...

... 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 said:
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 said:
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.WebControls.RequiredFieldValidator and attempting to cast it
into an IValidator and/or a BaseValidator. Both casting attempts have
failed miserably, throwing an InvalidCastException ... 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 RequiredFieldValidator 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 InvalidCastException. 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:]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top