当值明显为outlook.Version时,为什么error CS1061: 'Application' does not contain a definition for 'Version'会显示16.0.0.14326?
基于这个问题:Outlook“Class”不包含“Property”的定义
我想我已经把注意力集中在这里的问题上了。在“立即”窗口中打印outlook实例将产生以下结果:
{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"));
很有用,我很想更多地了解它的原因。例如,它在打印实例时返回完全相同的值:
{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示例时,我执行了以下步骤:
outlook2.Quit();上Console.WriteLine(outlook.Version);时,outlook.Version返回error CS1061: 'Application' does not contain a definition for 'Version'为什么向控制台写入值会影响正确返回属性的能力?
发布于 2022-08-04 09:01:00
实际上,我无法重现问题中的问题,而是调用支持COM自动化的COM对象的另一种方法(IDispatch接口、类型化等等)。例如,Microsoft应用程序将使用与C#动态关键字 4一起引入的C#。
因此,在Outlook示例中,您可以使用以下代码创建Outlook应用程序对象 (无需创建任何引用或使用任何PIA,只需正确安装Outlook ):
dynamic outlook = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));然后调用任何方法或属性,就像在不存在自动完成时一样:
Console.WriteLine(outlook.Version); // C# will dynamically call for a property named "Version"注意:名称解析发生在运行时,其主要缺点是没有自动完成,因此,对于Outlook这样的大模型层次结构,很难使用。
https://stackoverflow.com/questions/73216339
复制相似问题