Search code examples
c#.netdllenumslate-binding

Why can't enumerations from different DLLs be accessed through late binding?


I was trying to find out how I can access Excel's enumerations through COM interoperability using C#.

Its simple to do it using early binding but with late binding I only found out that I can access enums in the same file.

If the enumerations are in a different DLL they can't be accessed. So either I use the integer values or create my own enums.

Is it really not possible to access them through late binding? If so, why? I would expect that early binding is late binding code made easy by the IDE or so.


Solution

  • If the enumerations are in a different DLL they can't be accessed. So either I use the integer values or create my own enums.

    It is by definition, not possible to access pre-defined enums via late-binding. Obviously they will appear as "integers" to .NET. Sure you could cast some int to an enum that you have defined or perhaps a constant but such code is only for your benefit and does not represent the strong contract the COM library usually publishes.

    Is it really not possible to access them through late binding? If so, why? I would expect that early binding is late binding code made easy by the IDE or so.

    Early-binding makes use of either COM Type Libraries or COM Interop libraries. These are essentially .NET wrappers around COM types by providing C# or VB.NET-familiar types. With it you get intellisense in the form of statement completion; parameter help; and method help. The compiler will help you in any mistakes you might make at compile-time. Early-binding only works if the type library or COM interop library is present.

    Late-binding gives you nothing in the form of intellisense. There is no indication of what objects are available; what methods are present; nor what parameters to pass. Your code may compile but you could still get runtime errors. Late binding does not use nor require type libraries nor COM interop libraries.

    In addition, the term late binding means something quite specific to COM. It generally involves the invoking of IDispatch to get a list of method names. I'm not sure that .NET's enumType.GetField("Bar").GetValue() qualifies.

    Late Binding Example

    Late binding C# code:

    // You will get no intellisense help here
    var progId = "Excel.Application";
    dynamic excelApp = Activator.CreateInstance(Type.GetTypeFromProgID(progId));
    excelApp.Workbooks.Add = true;             // VS happily lets me type all this
    dynamic workSheet = excelApp.ActiveSheet;  // hope this all works at runtime
    

    Late binding or not

    Isn't this example accessing an enum via late-binding with reflection? The difference is that it only works for the same file

    Perhaps, it's a form of late binding. I would probably use the term decoupled.

    Late-binding in the COM world is generally used in one or more of the following scenarios:

    a) don't know what you will be interfacing with ahead of time

    b) you do know, but you don't have access to any type library either because its not installed or the developer never created it

    c) want to decouple your app from any specific version of the COM library

    The example you supplied which uses enumType.GetField("Bar").GetValue(null); tells me a few things:

    1. you know you are dealing with Excel
    2. you have access to a form of "type library" - one that contains definitions. In this case enum constants
    3. You are somewhat coupled to Excel

    With this in mind, I'm not sure why you want to follow a late-bound route. You seem to be taking the harder approach.

    Tell me more