I need to write a couple DLLs that will both be accessed from a C# application and a C++ application. Initially, I was thinking that I could save time/effort by writing the DLLs in C# and linking to them from the C# and C++ applications. Is this approach wise, or should the DLLs be written using C++?
问题:
回答1:
My advice would be to implement the code where you are most comfortable, but don't forget the interop requirement as you go. Know upfront what your interface is and what glue is required in the end product. Write the interface and test the interop before you fill out the implementation.
If the complexity of your requirement makes comfort level irrelevant, I would write it in C++ in such a way that you can access via P/Invoke.
One important factor is are you using C++/CLI or native C++. It's a lot easier to interop between C++/CLI and C#, in either direction.
回答2:
I suggest you write your DLLs in C++, expose their public classes to COM using ATL or similar, then have tlbimp generate a thin C# wrapper around the COM interfaces.
回答3:
If the DLL is to be used in other .Net applications, then you are pretty safe writing it in C# or C++, whichever you are more comfortable in.
If the DLL is to be used elsewhere, you will find it easier to write it in C++.