【问题标题】:How to add reference to Outlook VSTO in a dynamic CSharpCodeProvider script如何在动态 CSharpCodeProvider 脚本中添加对 Outlook VSTO 的引用
【发布时间】:2012-09-08 14:38:29
【问题描述】:

我正在尝试使用 xml 文件来确定应将哪些 Outlook 属性包含在通过 VSTO 插件代码执行的工作流中。

例子

xml 可能声明当前Outlook.MailItemSubject 是工作流所需的。我无法使用反射来使用其字符串名称“Subject”获取Subject 属性,因为MailItem 是一个接口而不是一个类。

我认为解决方案可能是创建和编译动态 C# 代码,按名称返回所需的属性...

问题:我一直无法确定如何找到正在运行的 Microsoft.Office.Interop.Outlook.dll 的位置,以便将其添加为对动态编译器的引用。尝试了很多组合,最后的尝试如下图。

CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
parameters.IncludeDebugInformation = false;
parameters.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(Outlook.MailItem)).Location);

【问题讨论】:

    标签: c# reference outlook vsto csharpcodeprovider


    【解决方案1】:
    parameters.ReferencedAssemblies.Add("Microsoft.Office.Interop.Outlook.dll");
    

    【讨论】:

    • 这似乎确实适用于 microsoft interop dll。互操作 dll 来自 gac,并且不存在于我的程序 dll 所在的文件夹中。也许,我的问题应该是如何获取 gac 文件夹路径?
    • @VisualMicro:您不需要 GAC 路径。只需按名称添加程序集,它将由编译器自动解析。这段代码等于运行csc /r:YourAssembly.dll,这是你的目标,不是吗?
    • 是的,但这会产生编译错误“找不到元数据文件”parameters.ReferencedAssemblies.Add("Microsoft.Office.Interop.Outlook.dll");
    • 还有一些可能会有所帮助的信息。 MailIteminterface 而不是 class。如果我只是添加对“MyAssembly.dll”的引用,我会收到以下错误。 Cannot find the interop type that matches the embedded interop type 'Microsoft.Office.Interop.Outlook.MailItem'. Are you missing an assembly reference?
    • @Visual:根据MSDN,此干扰位于“Microsoft.Office.Interop.Outlook.dll”中
    【解决方案2】:

    我现在有答案了。 abatishchev 让我有信心知道应该做什么,所以我尝试将 office dll 的编译器包含路径设置为CompilerOptions

    我仍然需要弄清楚如何为当前版本的 Outlook 获取这条路径,但这听起来并不难,否则我可能会为此打开一个新问题:)

    这是有效的代码。

      parameters.ReferencedAssemblies.Add("Microsoft.Office.Interop.Outlook.dll"); 
      parameters.CompilerOptions = "/lib:\"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Visual Studio Tools for Office\\PIA\\Office14\"";`
    

    感谢 abatishchev 的所有帮助

    更新:Outlook 插件使用嵌入式互操作程序集,这会导致此问题。所以不可能从加载的程序集中确定 office dll 的位置。 /lib路径需要通过其他方式解析。

    【讨论】:

    • 路径可能因机器而异,这个库也应该安装到 GAC 中,不是吗?
    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多