3

I'm currently getting this error from an InvalidCastException

[A]System.Data.Objects.ObjectResult`1[EDMXAssembly.ServiceStatus] cannot be cast to 
[B]System.Data.Objects.ObjectResult`1[EDMXAssembly.ServiceStatus]. 
Type A originates from 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089' in the context 'Default' at location 
'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.Entity\v4.0_4.0.0.0__b77a5c561934e089\System.Data.Entity.dll'. 
Type B originates from 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the 
context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.Entity\v4.0_4.0.0.0__b77a5c561934e089\System.Data.Entity.dll'.

Long and ugly!

I'm using reflection to invoke a method inside an assembly that is loaded at runtime. The method I'm invoking returns ObjectResult<T>. The error appears when I try to cast the result of the invocation to that.

I think this is due to the runtime loaded assmebly containing a reference to System.Data.Entity, as well as the assembly doing the reflection and casting; because these are two different references, the types are treated as not being the same. (How far out am I there?)

Is there any way around this issue? The method I'm invoking is on an ObjectContext, so I can't change what it returns.

Thanks

Edit for snippet:

public QueryResultSet<T> ObjectQuery<T>(string dataContext, string functionName, List<ObjectParameter> functionParameters)
{
    var context = htContexts[dataContext];
    ObjectResult<T> qResult = (ObjectResult<T>)context.GetType().InvokeMember(functionName, BindingFlags.InvokeMethod, null, context, functionParameters.ToArray());

    return qResult;
}

The error occurrs on the qResult line (I'm just mocking this really quickly up at the moment, hence lack of try-catch)

8
  • 4
    Show us a snippet of the code where you are invoking the method.
    – IAbstract
    Aug 17, 2011 at 14:12
  • 3
    I'm pretty sure your analysis is correct; types from two different loads of an assembly are considered different types.
    – dlev
    Aug 17, 2011 at 14:14
  • Have you tried using the var keyword and accessing the class properties? var foo = myClass.results(); Aug 17, 2011 at 14:15
  • @Jeremy: The invocation returns object, and then I need to cast to the right type. var will only allow me access to the standard object fields, won't it?
    – Andy Hunt
    Aug 17, 2011 at 14:21
  • I think you are correct in your analysis and it's possible due to that the assemblies might be loaded into different load contexts (ie Load vs LoadFrom context). I would recommend to try Fusion Log Viewer to get enhanced assembly resolve information. Note that the culprit here could be: EDMXAssembly.ServiceStatus Aug 17, 2011 at 14:25

1 Answer 1

1

This is a good example of why you need to keep interfaces in a common, separate library. You need to use an interface for ObjectResult. This way, your ObjectResult can have a common contract between your other assemblies.

Edit
If you are not able to apply any inheritance to the source ObjectResult you may have to perform more reflection over the object returned in order to convert it to your ObjectResult. Warning: this could be a costly conversion ...

...or refactoring to derive another solution.

3
  • How would I make an interface for ObjectResult in this instance? I'm not able to alter what ObjectContext returns. In theory, wouldn't ObjectResult would be a common contract between assemblies? but for there being two references to it here.
    – Andy Hunt
    Aug 17, 2011 at 15:04
  • If you have an ObjectResult in one assembly, it is not equal to the ObjectResult in another assembly.
    – IAbstract
    Aug 17, 2011 at 15:13
  • I've noticed. So there isn't really a way around this? Refactoring time, perhaps?
    – Andy Hunt
    Aug 17, 2011 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.