【问题标题】:Convert Early Bind Outlook to Late Bind OutlookConvert Early Bind Outlook to Late Bind Outlook
【发布时间】:2022-12-01 22:03:43
【问题描述】:

Ich have a specific code for Outlook that I would like to convert into late binding. However I am comparing some types, so I don't know how to make that comparison without having the dll loaded already.

var ol = (dynamic)Marshal.GetActiveObject("Outlook.Application");
dynamic windowType = ol.Application.ActiveWindow();
if (windowType is Microsoft.Office.Interop.Outlook.Explorer) {
    Console.WriteLine("success")
}

I am not able to compile the code if I have not loaded the interop dll for Outlook. So I will have to add

using Microsoft.Office.Interop.Outlook

Which I do not want to use because I won't know exactly what outlook version will be installed on the machine. This is also the reason why I would want to use late Binding.

I tried to get the type with

Console.WriteLine($"This is my type {windowType.GetType()}");

But I would only get the result

This is my type System.__ComObject

Any Ideas how I can late bind outlook and still make type comparisons? Can specific types be loaded for comparison?

【问题讨论】:

    标签: c# outlook interop office-interop com-interop


    【解决方案1】:

    It sounds like you want to use late binding to avoid having to compile your code with a specific version of the Outlook interop assembly. Late binding allows you to avoid specifying the type of an object at compile time, but instead determine the type at runtime.

    To compare the type of an object using late binding, you can use the Type.GetType() method. This method takes the name of the type you want to compare the object to, and returns a Type object representing that type. You can then use the Type.IsInstanceOfType() method to compare the object to the type you specified.

    Here is an example of how you could use late binding to compare the type of the windowType object:

    Type outlookExplorerType = Type.GetType("Microsoft.Office.Interop.Outlook.Explorer, Outlook");
    if (outlookExplorerType.IsInstanceOfType(windowType)) {
        Console.WriteLine("success");
    }
    

    In this example, the Type.GetType() method is used to get the Type object representing the Microsoft.Office.Interop.Outlook.Explorer type. The Type.IsInstanceOfType() method is then used to compare the windowType object to the Microsoft.Office.Interop.Outlook.Explorer type.

    It's worth noting that using late binding in this way can make your code more difficult to read and maintain, so it's generally recommended to avoid it if possible. It may be better to use a version of the Outlook interop assembly that is compatible with the version of Outlook installed on the machine where your code will be running.

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 1970-01-01
      • 2011-08-01
      • 2019-05-03
      • 2022-12-02
      • 1970-01-01
      • 2022-07-09
      • 2019-08-29
      • 1970-01-01
      相关资源
      最近更新 更多