Casting error with class obtained through reflection

Posted on 2004-10-28
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:

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 )
Question by:athomas4219
    LVL 5

    Expert Comment


    Check it out, maybe you are experiencing what I been

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

    Author Comment

    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?

    Author Comment

    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?
    LVL 7

    Expert Comment

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

    Author Comment

    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.


    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles al…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    911 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now