在 Microsoft Visual Studio Tools for Office (VSTO) 3 (C#) 中传递参数的最佳实践
许多与 VSTO 中的 Office 对象模型交互的参数都需要通过引用传递的对象参数,即使参数的名义类型是 int 或 string。
- 我认为使用这种机制以便代码可以修改参数,尽管我无法弄清楚为什么这些需要作为通用对象而不是作为更合适的类型传递。 谁能启发我吗?
我一直在使用的机制(抄袭自帮助和 MSDN 资源)本质上是创建一个包含适当数据的通用对象,然后将其传递给方法,例如:
object nextBookmarkName = "下一个内容"; 对象 nextBookmark = this.Bookmarks.get_Item( ref nextBookmarkName ).Range;
Microsoft.Office.Interop.Word.Range newRng = this.Range( ref nextBookmark, ref nextBookmark );
这似乎有很多额外的代码,但我看不出更好的方法来做到这一点。 我确信我错过了一些东西; 它是什么? 或者这真的是最佳实践吗?
Many of the parameters for interacting with the Office Object model in VSTO require object parameters that are passed by reference, even when the notional type of the parameter is an int or string.
- I suppose that this mechanism is used so that code can modify the parameter, although I can't figure out why these need to be passed as generic object instead of as their more appropriate types. Can anyone enlighten me?
The mechanism I've been using (cribbed from help and MSDN resources) essentially creates a generic object that contains the appropriate data and then passes that to the method, for example:
object nextBookmarkName = "NextContent";
object nextBookmark = this.Bookmarks.get_Item( ref nextBookmarkName ).Range;Microsoft.Office.Interop.Word.Range newRng = this.Range( ref nextBookmark, ref nextBookmark );
This seems like a lot of extra code, but I can't see a better way to do it. I'm sure I'm missing something; what is it? Or is this really the best practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我同意乔的观点。 我什至开发了像这样的辅助结构和类:
还有这个:
还有更多......
I agree with Joe. I even developed helper structs and classes like this one:
And this one:
And a few more...
我也会对此感兴趣。 我正在编写几个在 Word 中使用自动化的应用程序,我什至有类似这样的事情
,这非常令人讨厌,但这是迄今为止我所知道的唯一方法。
我唯一能想到的就是为常用的函数编写一个包装类......
I'd be interested in this too. I'm coding several apps that uses automation in Word and I even have things like
It's very nasty, but it's the only way I know so far.
The only thing I can think of is writing a wrapper class for the frequently used functions...
我认为这只是原始 Word 对象模型的糟糕设计。 我知道在 COM 世界中通过引用传递字符串可能会稍微快一些,因为它避免了复制的需要,所以也许这就是理由的一部分。 但缺点是被调用者可以修改该值,并且在大多数情况下,对于 Word,它们是输入参数。
我认为你的技术是最好的实践。 对于许多 Word 对象模型方法所需的数百万个可选参数,您可以创建一个“缺失”的静态字段,例如:
objectmissing = Type.Missing;
// 例子
对象文件名 = ...
document.SaveAs(ref fileName, ref 缺失, ref 缺失, ref 缺失,
参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,
参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,
参考缺失,参考缺失);
I think it was just poor design of the original Word object model. I know that passing strings by reference can be slightly faster in the COM world because it avoids the need to make a copy, so perhaps that was part of the justification. But the downside is that the callee can modify the value, and in most cases with Word they are input parameters.
I think your technique is the best practice. For the millions of optional parameters that many of the Word object model methods require, you can create a single static field "missing" something like:
object missing = Type.Missing;
// Example
object fileName = ...
document.SaveAs(ref fileName, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing);
我认为,所有这些都可以通过 VS.NET 2010 和 c# 4.0 中引入的新语言结构来解决(c# 将有可选参数)。
请参阅 Channel9 上 Anders Hejlberg 在 PDC 2008 上的视频,了解与办公室开发相关的变化。
我找不到该链接,但这也可能有帮助。
http ://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friend/
I think, all of this is taken care of with VS.NET 2010 and new language constructs introduced in c# 4.0 (c# will have optional arguments).
See the video by Anders Hejlberg at PDC 2008 on channel9 for changes related to office development.
I cant find that link but this could also be helpful.
http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/