VSTO:操纵COM对象(“一个点好,两个点坏”) [英] VSTO: manipulating COM objects ("one dot good, two dots bad")

查看:119
本文介绍了VSTO:操纵COM对象(“一个点好,两个点坏”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Excel VBA背景,我会经常编写以下代码:

Coming from an Excel VBA background I would frequently write code such as:

Range("myRange").Offset(0, 1).Resize(1, ccData).EntireColumn.Delete

我现在要移至VSTO,并一直在阅读有关RCW计数器等的知识,以及显式释放COM对象的需求。基本建议似乎是:不要将对Excel对象的引用链接在一起(如我上面所述),因此一个点好,两个点坏。
我的问题是,我是否正确认为上述代码不是VSTO中的方法?如果是这样,那是否意味着我需要显式声明上述链中暗含的3个范围(偏移量,调整大小和整个列)?

I'm now moving to VSTO, and have been reading about RCW counters, etc., and the need to explicitly release COM objects. The basic advice seems to be: don't chain together references to Excel objects (as I have above) - hence "one dot good, two dots bad". My question is, am I correct that the above code is not the way to go in VSTO? If so, does that mean that I would need to explicitly declare the 3 ranges implied in the above chain (Offset, Resize & EntireColumn)?

像:

rng.Columns.Count

其中rng是声明的Range?我应该为rng.Columns分配一个名称,以获得范围内的列数吗?

where rng is a declared Range? Should I be assigning a name to rng.Columns in order to obtain the number of columns in the range??

推荐答案

两点规则愚蠢背后非常有害的货物崇拜,它完全无法使C#程序员自版本4起摆脱麻烦。它比使Office程序按需退出的简单方法

There is very detrimental cargo cult behind that "two dot rule" silliness, it completely fails to keep C# programmers out of trouble since version 4. And it endlessly more painful than the simple way to make Office programs quit on demand.

但这完全不是您遇到的问题 ,该货物崇拜仅适用于使用自动化激活Office程序的过程外程序。实际上,您的代码在Office程序内运行( ),您当然不在乎程序何时终止。因为那也会终止您的代码。

But this is not a problem you have at all, the cargo cult only applies to an out-of-process program that use Automation to activate an Office program. Your code in fact runs inside the Office program, you of course don't care when the program terminates. Because that terminates your code as well.

只需按照编写常规C#代码的方式编写代码,GC就不需要任何帮助。

Just write your code the way you'd write regular C# code, the GC does not need any help.

这篇关于VSTO:操纵COM对象(“一个点好,两个点坏”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆