.NET 4.0 beta 2 中的 PIA 嵌入是否已损坏?
不久前,我在 Visual Studio beta 1 中编写了一些 Word 互操作示例,并将对 Microsoft.Office.Interop.Word
的引用设置为嵌入(在参考属性)。这些工作正常,我已经有一段时间没有运行它们了......直到今天。
当然,现在我正在 Visual Studio 2010 和 .NET 4.0 的 beta 2 下运行 - 而且它似乎有些损坏。
这是有问题的代码(只是虚拟示例代码):
using Microsoft.Office.Interop.Word;
class WordImprovement1
{
static void Main()
{
Application app = new Application { Visible = true };
app.Documents.Add();
Document doc = app.ActiveDocument;
Paragraph para = doc.Paragraphs.Add();
para.Range.Text = "Thank goodness for C# 4";
object filename = "demo.doc";
object format = WdSaveFormat.wdFormatDocument97;
doc.SaveAs(FileName: ref filename, FileFormat: ref format);
doc.Close();
app.Quit();
}
}
这是当“嵌入互操作类型”设置为“true”或我与“/l”链接时大多数出现的异常命令行:
System.MissingMethodException: Method not found:
'Void Microsoft.Office.Interop.Word._Application.set_Visible(Boolean)'.
at WordImprovement1.Main()
偶尔它会起作用——这更奇怪。
如果我将“嵌入互操作类型”设置为“false”(或在命令行上使用 /r 而不是 /l),则一切正常。
如果我删除“Visible = true”属性设置器,它也可以工作...但我知道该属性在那里...它甚至是由 IntelliSense 建议的!
虽然我还没有对多个机器进行详尽的测试,但我可以确认我的上网本(运行 Windows 7 而不是 Vista,但仍然是 .NET 4.0 beta 2)遇到了同样的问题。
关于到底是我坏了还是 .NET 4.0 beta 2 有什么建议吗?
A while ago, I wrote some Word interop examples in Visual Studio beta 1, and set the reference to Microsoft.Office.Interop.Word
to be embedded (set the "Embed Interop Types" = true in the reference properties). These worked fine, and I haven't run them for a while... until today.
Of course, now I'm running under beta 2 of both Visual Studio 2010 and .NET 4.0 - and it seems to be somewhat broken.
Here's the code in question (just dummy example code):
using Microsoft.Office.Interop.Word;
class WordImprovement1
{
static void Main()
{
Application app = new Application { Visible = true };
app.Documents.Add();
Document doc = app.ActiveDocument;
Paragraph para = doc.Paragraphs.Add();
para.Range.Text = "Thank goodness for C# 4";
object filename = "demo.doc";
object format = WdSaveFormat.wdFormatDocument97;
doc.SaveAs(FileName: ref filename, FileFormat: ref format);
doc.Close();
app.Quit();
}
}
Here's the exception I get most of the time, when "Embed Interop Types" is set to "true" or I link with "/l" on the command line:
System.MissingMethodException: Method not found:
'Void Microsoft.Office.Interop.Word._Application.set_Visible(Boolean)'.
at WordImprovement1.Main()
Very occasionally, it works - which is even more bizarre.
If I set "Embed Interop Types" to "false" (or use /r on the command line instead of /l) it all works fine.
If I remove the "Visible = true" property setter it works too... but I know that property's there... it's even suggested by IntelliSense!
While I haven't done exhaustive testing on multiple boxes, I can confirm that my netbook (running Windows 7 instead of Vista, but still .NET 4.0 beta 2) sees the same problem.
Any suggestions as to whether it's me that's broken or .NET 4.0 beta 2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很棒的收获!这看起来像是我们的索引属性代码生成中的一个错误(不是已知的错误,所以感谢您报告这一点!)我已经尝试过 VS 2010 的最新内部版本,它也重现了。
发生的情况是,如果您使用对象初始值设定项,则会生成以下代码:
但是,如果您在单独的语句中设置 Visible = true(没有对象初始值设定项),则会发出以下代码:
PIA 仅定义底部一个,因此当您使用对象初始值设定项时它会失败。目前的解决方法是在单独的语句中设置 Visible,这应该可以正常工作。
我已经记录了一个关于此的错误。再次感谢!
Great catch! This looks like a bug in our code generation for indexed properties (not a known one, so thanks for reporting this!) I've tried on the latest internal build of VS 2010 and it reproduces as well.
What happens is that if you use an object initializer, then the following code is generated:
However if you set Visible = true in a separate statement (without the object initializer), then the following code is emitted:
The PIA only defines the bottom one, hence it fails when you're using an object initializer. The workaround for now is to set Visible in a separate statement which should work fine.
I've logged a bug about this. Thanks again!