Type 'T' incompatible with type 'T'

I have two libraries written in C # that I would like to use in an F # application. Both libraries use the same type, however I cannot convince F # the type of verification of this fact.

Here is a simple example of using interaction types with Office. F # seems especially sensitive to these types of problems. Casting on the F # side doesn't seem to help the situation. All three projects have a link to the same assembly ("Microsoft.Office.Interop.Excel", version 14.0.0.0).

In project Project1 (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 (C # project):

 namespace Project2 { public class Class2 { Microsoft.Office.Interop.Excel.Application _app; public Class2(Microsoft.Office.Interop.Excel.Application app) { _app = app; } } } 

In the project "TestApp" (project F #):

 [<EntryPoint>] let main argv = let c2 = Project2.Class2(Project1.Class1.GetApp()) 0 

Any clues?

Edit:

Changing the call to the Class2 constructor with the following dynamic push solves the problem:

 let c2 = Project2.Class2(Project1.Class1.GetApp() :?> Microsoft.Office.Interop.Excel.Application) 

However, this is unsatisfactory, since it is 1) dynamic and 2) I still do not understand why the original type check failed.

+7
c # f # office-interop
source share
1 answer

COM interoperability. When you reference a COM assembly, it will actually create a COM interop assembly for marshaling between .NET and COM. If you have two assemblies that reference the same COM assembly, you can actually have two identically generated connection assemblies.

One solution and, frankly, the best design overall would be to create interfaces in one of two assemblies (or a shared third assembly) that display the functions you want to publish and use these interfaces instead of using or using COM types.

 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... } } } 
+2
source share

All Articles