I'm working on integrating an accounting system which objects are COM objects.
When binding one to one as follows, it works just fine.
IKernel kernel = new StandardKernel();
kernel.Bind<IAcoSDKX>().ToMethod(_ => { return new AcoSDKX(); }).InSingletonScope();
The situation I'm having is that both IAcoSDKX
and AcoSDKX
are interfaces, and the AcoSDKClass
is inaccessible to the consumer.
So I'm looking for a way to bind both interfaces together as only their spelling differ. Ont starts with an 'I', and other doesn't. So I'd like to come up with a conventional binding where even though I keep using unbound interfaces, Ninject knows what to bind it to when activating the objects through constructor injection.
Here's the try I came up with so far with no success.
kernel.Bind(services => services
.FromAssembliesMatching("AcoSDK.dll")
.SelectAllTypes()
.Where(t => t.Name.StartsWith("I") && t.IsInterface)
.BindDefaultInterfaces());
So I wonder, how to configure a convention binding using Ninject that could suit the actual need?
Basically the convention is to bind all interfaces starting with "I" with interfaces having the same name without the "I" prefix.
EDIT
After further searches, I found out that the types of AcoSDK.dll are embedded into my own assembly. Only types that are early loaded are bindable.
Besides, although I can new a COM Object interface, the Activator.CreateInstance won't initialize it under pretext that it is an interface. See objects declaration as follows:
namespace AcoSDK {
[ComImport]
[Guid("00000114-0000-000F-1000-000AC0BA1001"]
[TypeLibType(4160)]
public interface IAcoSDKX { }
[ComImport]
[Guid("00000114-0000-000F-1000-000AC0BA1001")]
[CoClass(typeof(AcoSDKClass))]
public interface AcoSDKX : IAcoSDKX { }
[ComImport]
[Guid("00000115-0000-000F-1000-000AC0BA1001")]
[TypeLibType(2)]
[ClassInterface(0)]
public class AcoSDKXClass : IAcoSDKX, AcoSDKX { }
}
public class Program() {
// This initializes the type.
IAcoSDKX sdk = new AcoSDKX();
// This also does.
IKernel kernel = new StandardKernel();
kernel.Bind<IAcoSDKX>().ToMethod(_ => new AcoSDKX());
// This won't activate because type is an interface.
IAcoSDKX sdk = Activator.CreateInstance(typeof(AcoSDKX));
// This says : Interop type AcoSDKXClass cannot be embedded. Use the applicable interface instead.
IAcoSDKX sdk = Activator.CreateInstance(typeof(AcoSDKXClass));
// Same message occurs when newing.
IAcoSDKX sdk = new AcoSDKClass();
}
Given this new information, anyone has a similar experience with COM Objects? I tried to new the AcoSDKClass
or to access it in any fashion, and I just don't seem to get a hook on it.