首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#类实例显示属性值,但不允许直接引用

C#类实例显示属性值,但不允许直接引用
EN

Stack Overflow用户
提问于 2022-08-03 04:36:23
回答 1查看 100关注 0票数 1

当值明显为outlook.Version时,为什么error CS1061: 'Application' does not contain a definition for 'Version'会显示16.0.0.14326

基于这个问题:Outlook“Class”不包含“Property”的定义

我想我已经把注意力集中在这里的问题上了。在“立即”窗口中打印outlook实例将产生以下结果:

代码语言:javascript
运行
复制
{Microsoft.Office.Interop.Outlook.ApplicationClass}
    AnswerWizard: '((Microsoft.Office.Interop.Outlook.ApplicationClass)outlook).AnswerWizard' threw an exception of type 'System.Runtime.InteropServices.COMException'
    Application: {Microsoft.Office.Interop.Outlook.ApplicationClass}
    Assistance: {System.__ComObject}
    Assistant: {System.__ComObject}
    COMAddIns: {System.__ComObject}
    Class: olApplication
    DefaultProfileName: "Outlook"
    Explorers: {Microsoft.Office.Interop.Outlook.ExplorersClass}
    FeatureInstall: msoFeatureInstallOnDemand
    Inspectors: {Microsoft.Office.Interop.Outlook.InspectorsClass}
    IsTrusted: false
    LanguageSettings: {System.__ComObject}
    Name: "Outlook"
    Parent: null
    PickerDialog: {System.__ComObject}
    Reminders: {Microsoft.Office.Interop.Outlook.RemindersClass}
    Session: {Microsoft.Office.Interop.Outlook.NameSpaceClass}
    TimeZones: {Microsoft.Office.Interop.Outlook.TimeZonesClass}
    Version: "16.0.0.14326"

但是,尝试打印outlook实例的任何属性,甚至那些看起来应该给出像outlook.Version这样简单的值的属性,都会给出does not contain a definition for错误。Intellisense愉快地自动填充这些属性,其中至少有一些显然具有值。到底怎么回事?

以下是@SimonMourier建议使用的最新信息:

dynamic outlook = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));

很有用,我很想更多地了解它的原因。例如,它在打印实例时返回完全相同的值:

代码语言:javascript
运行
复制
{Microsoft.Office.Interop.Outlook.ApplicationClass}
    AnswerWizard: '((Microsoft.Office.Interop.Outlook.ApplicationClass)outlook).AnswerWizard' threw an exception of type 'System.Runtime.InteropServices.COMException'
    Application: {Microsoft.Office.Interop.Outlook.ApplicationClass}
    Assistance: {System.__ComObject}
    Assistant: {System.__ComObject}
    COMAddIns: {System.__ComObject}
    Class: olApplication
    DefaultProfileName: "Outlook"
    Explorers: {Microsoft.Office.Interop.Outlook.ExplorersClass}
    FeatureInstall: msoFeatureInstallOnDemand
    Inspectors: {Microsoft.Office.Interop.Outlook.InspectorsClass}
    IsTrusted: false
    LanguageSettings: {System.__ComObject}
    Name: "Outlook"
    Parent: null
    PickerDialog: {System.__ComObject}
    Reminders: {Microsoft.Office.Interop.Outlook.RemindersClass}
    Session: {Microsoft.Office.Interop.Outlook.NameSpaceClass}
    TimeZones: {Microsoft.Office.Interop.Outlook.TimeZonesClass}
    Version: "16.0.0.14326"

那么,他们为什么不以同样的方式返回属性值呢?为什么我在new Outlook.Application()中得到定义错误,而Activator.CreateInstance()没有定义错误

在为@SimonMourier创建MVC示例时,我执行了以下步骤:

  1. 新的C# .NET框架4.7.2控制台桌面应用程序项目
  2. 添加引用-> COM ->Outlook16.0对象库
  3. 将以下代码添加到Main()中: 动态Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));outlook2 =新Microsoft.Office.Interop.Outlook.Application();Console.WriteLine(outlook.Version);outlook2.Quit();outlook.Quit();
  4. 中断在outlook2.Quit();
  5. 如前所述,outlook.Version正确地返回"16.0.0.14326“
  6. 在注释掉Console.WriteLine(outlook.Version);时,outlook.Version返回error CS1061: 'Application' does not contain a definition for 'Version'
  7. returns总是正确地返回"16.0.0.14326“

为什么向控制台写入值会影响正确返回属性的能力?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-04 09:01:00

实际上,我无法重现问题中的问题,而是调用支持COM自动化的COM对象的另一种方法(IDispatch接口、类型化等等)。例如,Microsoft应用程序将使用与C#动态关键字 4一起引入的C#。

因此,在Outlook示例中,您可以使用以下代码创建Outlook应用程序对象 (无需创建任何引用或使用任何PIA,只需正确安装Outlook ):

代码语言:javascript
运行
复制
dynamic outlook = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));

然后调用任何方法或属性,就像在不存在自动完成时一样:

代码语言:javascript
运行
复制
Console.WriteLine(outlook.Version); // C# will dynamically call for a property named "Version"

注意:名称解析发生在运行时,其主要缺点是没有自动完成,因此,对于Outlook这样的大模型层次结构,很难使用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73216339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档