c# - The type 'T' is not compatible with the type 'T' -
i have 2 libraries written in c# i'd use in f# application. both libraries use same type, however, unable convince f#'s type checker of fact.
here's simple example using office interop types. f# seems particularly sensitive these type issues. casting on f# side not seem situation. 3 projects have reference same assembly ("microsoft.office.interop.excel" version 14.0.0.0).
in project "project1" (a c# project):
namespace project1 { public class class1 { public static microsoft.office.interop.excel.application getapp() { return new microsoft.office.interop.excel.application(); } } }
in project "project2" (a c# project):
namespace project2 { public class class2 { microsoft.office.interop.excel.application _app; public class2(microsoft.office.interop.excel.application app) { _app = app; } } }
in project "testapp" (an f# project):
[<entrypoint>] let main argv = let c2 = project2.class2(project1.class1.getapp()) 0
any hints?
edit:
changing call class2
's constructor following dynamic cast solves problem:
let c2 = project2.class2(project1.class1.getapp() :?> microsoft.office.interop.excel.application)
however, unsatisfying, since 1) dynamic, , 2) still don't understand why original type check failed.
com interop. when reference com assembly generate com interop assembly marshal between .net , com. if have 2 assemblies both referencing same com assembly may have 2 identically generated interop assemblies.
one solution, , frankly better design in general, create interfaces in 1 of 2 assemblies (or shared 3rd assembly) expose features want make public , instead of using or consuming com types use these interfaces instead.
namespace project1 { public interface iapplication { // application members here... } public class class1 { public static iapplication getapp() { return new excelapplication(new microsoft.office.interop.excel.application()); } private class excelapplication : iapplication { public excelapplication(microsoft.office.interop.excel.application app) { this.app = app; } // implement iapplication here... } } }
Comments
Post a Comment