找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1007|回复: 0

[分享] How to pass COM optional parameter in C#

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-26 18:20:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
How to pass COM optional parameter in C#                                                        By Philippe Leefsma
  Here is a problem you may encounter when invoking the AutoCAD ActiveX API from C#:
  I'm trying to use AcadSelectionSet.SelectOnScreen in C# with similar approaches I've been using in VB. But I can't omit the optional parameters in C# which I can in VB.      
I tried to create some empty arrays and pass them to SelectOnScreen but I get invalid argument errors.      
Could you offer a solution?

  
  Solution
  Note: this workaround only applies to .Net 3.5 and prior versions of the framework, as C# 4.0 provides missing parameters functionality.
  In VB, you can ignore the optional parameters when you call AcadSelectionSet.SelectOnScreen but you can't do that in C#. This is because C# doesn't support optional parameters. Also, if you really want to pass NULL values as an indication that you don't want any values in the object array (no variant in C#), you can't create arrays with values, the zeros will be trades as values in the DXF code not 'nothing'. That's where the problem is.
  Fortunately, there is a solution in .NET which is using object reflection to specific a missing value that is NULL.   
   object o = System.Reflection.Missing.Value;
  This will be the equivalent of NULL values passed into VB functions call. The following code snippet demonstrates how it works.
  1. //
  2. // error checking code omitted for code brevity
  3. //
  4. private void TestMissing(AcadApplication oAcad)
  5. {
  6.     AcadDocument oDoc = oAcad.ActiveDocument;
  7.     AcadSelectionSet ss = oDoc.SelectionSets.Add("ss01");
  8.     object oMissing01 = System.Reflection.Missing.Value;
  9.     object oMissing02 = System.Reflection.Missing.Value;
  10.     ss.SelectOnScreen(oMissing01, oMissing02);
  11.     System.Windows.Forms.MessageBox.Show(
  12.         "Number of objects in the selection: " + ss.Count + ".");
  13.     ss.Delete();
  14. }
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-6 12:13 , Processed in 0.316233 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表