Problem with reflection casting in .NET 2.0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TheLongshot@gmail.com

    Problem with reflection casting in .NET 2.0

    I'm in the process of converting a VS 2003 app to VS 2005. Right now
    I've run into a problem which I can't figure out. The following
    statement:

    return
    (IDataProviderB ase)(((Construc torInfo)cache["IDataProviderB ase"]).Invoke(null)
    );

    worked fine in 2003, but gives me an InvalidCastExce ption in 2005. The
    object getting created is derived from IDataProviderBa se, and when I
    try to cast to the object's class, I still get an InvalidCastExce ption.

    All of this is in the same DLL, so there shouldn't be any versioning
    issues as far as I know.

    Any clue what the problem is?

    Jason

  • Marc Gravell

    #2
    Re: Problem with reflection casting in .NET 2.0

    I think you should break this line into sections and see where the exception
    is actually happening. If (as you say) the object created does support that
    interface, then this shouldn't be the issue. My guess would be somewhere in
    the cache[string] code that you didn't show...

    Marc


    Comment

    • TheLongshot@gmail.com

      #3
      Re: Problem with reflection casting in .NET 2.0

      I did break it down to cast it to the actual object it was creating,
      and it was still giving me the exception. For some reason, I can't
      cast the object even to itself.

      Really, I'm stumped here.

      Jason

      Marc Gravell wrote:
      I think you should break this line into sections and see where the exception
      is actually happening. If (as you say) the object created does support that
      interface, then this shouldn't be the issue. My guess would be somewhere in
      the cache[string] code that you didn't show...
      >
      Marc

      Comment

      • Marc Gravell

        #4
        Re: Problem with reflection casting in .NET 2.0

        I wonder... you wouldn't have this interface declared (separately) in two
        asemblies, would you? It doesn't matter if they are named the same and have
        the same source code: if they are in different assemblies they *are*
        different interfaces...

        Assuming this isn't the problem, what happens if you put:
        object obj = ((ConstructorIn fo)cache["IDataProviderB ase"]).Invoke(null);
        Debug.WriteLine (obj.GetType(). FullName);
        then do you get what you expect? Alternatively you could use a break
        point...

        Marc


        Comment

        Working...