Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Casting error with class obtained through reflection

Posted on 2004-10-28
7
Medium Priority
?
239 Views
Last Modified: 2010-04-15
Below is a snippet of code that I'm having problems with. When I reach the line containing the foreach loop, I get a casting exception. Here's what's going on... First, I am certain that the objects contained in the commissions ArrayList implement the ICalcuTreeCommission interface. Second, the call creating the ICalcuTreeCalculator uses reflection to obtain a reference to an object implementing this interface. The problem, I think, lies within the following article:

http://www.gotdotnet.com/team/clr/LoadFromIsolation.aspx

However, my question is this: how can I get around this? We are using this code to view commissions produced by different types of commission calculators. Although each commission is different, they all have certain things in common, such as when they were run and their amount. It is these kinds of things that I am looking to display using the interface and the reflection.

Finally, here's one final bizarre twist: the code below runs just fine when I put it into a console application. It only fails when I have it in an ASCX control in an ASP.NET web page. Any thoughts? Any solutions? Thanks!

ICalcuTreeCalculator calc = calculator.GetCalculator();
ArrayList commissions = calc.GetCommissions( distributor.Id );
foreach( ICalcuTreeCommission commission in commissions )
0
Comment
Question by:athomas4219
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:Kelmen
ID: 12441267
http://www.google.com/groups?hl=en&lr=lang_zh-CN|lang_zh-TW|lang_en&newwindow=1&c2coff=1&th=de7957ca93cc5a93&rnum=10

Check it out, maybe you are experiencing what I been

BTW, are yours dynamic assemblies and the interface strongnamed / GAC ?
0
 

Author Comment

by:athomas4219
ID: 12443264
I don't know about strongnamed...I've heard the term, but don't know what it means or how to check. As for the GAC, the assemblies we're trying to load are found in the bin directory of the ASP.NET application. However, and I don't know if this makes a difference, the assemblies are found in the same directory as the executable in the case of the console app. Could this have something to do with it in any way? If so, how/where do I put the assemblies for the web app to remove this error?
0
 

Author Comment

by:athomas4219
ID: 12443978
After doing a little digging into strong naming, I'm pretty sure that my assembly isn't strongly named. I would check in the AssemblyInfo.cs file, correct? I used sn.exe to generate a strong name and tried to use that file, but it then started complaining that every assembly I referenced in the project wasn't strongly named. Is this a requirement for a strongly named assembly?

Finally, if anyone is relatively sure that this will solve my problem I will look at getting those assemblies strongly named, but it's a lot of work (I didn't write them so I have to track down their authors for sourcecode) so I'd like to be reasonably sure that it would make a different before continuing.

Finally, I am still confused as to why this code would work with a console application but not with an ASP.NET application. Thoughts?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12445185
I experienced a similiar issue between a windows/console app, a service and a web page.  What I found is that for the windows app you can expect the working directory to be the same as where the executable is... However, when running a service or a web application (page, control, service) the working directory was system32 in the wondows directory...  A quick way to test this is to put your dlls in that directory and see of your code works... if it does this is the issue, otherwise it is something else...

If this is the issue you will need to give a full path to the dlls... note this should not be relative and it can not be gathered from the environment!  The way I solved this was to create a base direcotry for my application and set that in the app/web .config file... I then prepended this to all assemblies that I want to dynamically load.

Hope this helps...
0
 

Author Comment

by:athomas4219
ID: 12445344
Currently we have a database column that specifies the name of the assembly to load. When it comes time to load it, we run the following line of code:

Assembly a = Assembly.LoadFile( System.AppDomain.CurrentDomain.BaseDirectory + @"bin/" + assemblyName );

This code doesn't fail, and if I use debugging and stop points on the application I can see that my ArrayList is filled with 15 objects, exactly as I'd expect. This shows me that the assembly is loading correctly and running correctly.

One further strangeness: I used reflection to give me a list of interfaces supported by this object. Using GetType().GetInterfaces() I get only one...the one that I'm trying to cast to.

What I finally did, and this is only a workaround, is to use reflection to get the property that I know I want and display it that way. It works just fine, but isn't elegant and isn't in the spirit of everything. So, for the time being at least, I have it working. However, I STILL like to figure out the problem, so if anyone has any other suggestions or things for me to try, I'm all about finding a solution.

Thanks!
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12663729
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question