473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reflection / Casting Question

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:]
Jul 21 '05 #1
2 1759
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.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:]

Jul 21 '05 #2
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[i];

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[i] = (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.Cl assName);

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[i];

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" <Jo********@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.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.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:]

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
1
by: Roman Muntyanu | last post by:
Hi all, I created object of MyClass : ISomeInterface Then I created object of this class using reflection mechanism. ISomeInterface instance = (ISomeInterface)constructorInfo.Invoke(new...
2
by: cv | last post by:
Hi, I want to create an object from a class I load from an assembly. Then I want to cast that object to a class (an interface) 'known' to the program in order to pass it to other methods that work...
2
by: Angelos Karantzalis | last post by:
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...
7
by: Paul Hadfield | last post by:
Hi, I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
5
by: | last post by:
I am having problems with casting or converting a class to to an interface from which it derives. I'm certain that it's due to how it's being loaded, but I'm not sure how to get past the problem....
2
by: Matt | last post by:
Hi, Is it possible to write a method which can take both a normal instansiated type and reflection instansiated type? For example, the following code doesn't work. It throws a casting...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.