找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1084|回复: 0

[分享] Command Method: Static Or Not Static

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-23 21:49:22 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 csharp 于 2014-8-6 09:21 编辑

Command Method: Static Or Not Static

When starting to use AutoCAD .NET API, the very first code people would be writing is the the command method in a class, e.g. a public method decorated with [CommandMethod()] attribute. However, the command method can be either "static/Shared" (C#/VB.NET) or not "static/Shared" (instance method). Also, in most smaple code one can find, there is rarely one that show the class' constructor is used.


It also puzzles some beginners if the command method is not a "static" one: why an instance method (non-static method) can be called in command without the code creating an object instance of the class?


Here is what the AutoCAD .NET API document has to say:



For an instance command method, the method's enclosing type is instantiated separately for each open document. This means that each document gets a private copy of the command's instance data. Thus there is no danger of overwriting document-specific data when the user switches documents. If an instance method needs to share data globally, it can do so by declaring static or Shared member variables.
For a static command method, the managed wrapper runtime module does not need to instantiate the enclosing type. A single copy of the method's data is used, regardless of the document context. Static commands normally do not use per-document data and do not require special consideration for MDI mode.



  
With a few lines of very simple code, we can see what the difference betweem static method and instance command method.
  
Here is the code:


  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.Runtime;
  3. namespace CommandClassTest
  4. {
  5.     public class MyCommand
  6.     {
  7.         private static int _staticCount = 0;
  8.         private int _instanceCount = 0;
  9.         public MyCommand()
  10.         {
  11.             Document dwg = Application.DocumentManager.MdiActiveDocument;
  12.             dwg.Editor.WriteMessage("\n**************");
  13.             dwg.Editor.WriteMessage("\nConstructing...");
  14.             dwg.Editor.WriteMessage("\n**************");
  15.         }
  16.         [CommandMethod("StaticCommand", CommandFlags.Session)]
  17.         public static void RunThisMethod1()
  18.         {
  19.             _staticCount++;
  20.             Document dwg = Application.DocumentManager.MdiActiveDocument;
  21.             dwg.Editor.WriteMessage("\n----------------------------");
  22.             dwg.Editor.WriteMessage("\n" + dwg.Name);
  23.             dwg.Editor.WriteMessage("\nStatic command executed.");
  24.             dwg.Editor.WriteMessage("\nStatic command count: {0}", _staticCount);
  25.         }
  26.         [CommandMethod("InstanceCommand", CommandFlags.Session)]
  27.         public void RunThisMethod2()
  28.         {
  29.             _instanceCount++;
  30.             Document dwg = Application.DocumentManager.MdiActiveDocument;
  31.             dwg.Editor.WriteMessage("\n----------------------------");
  32.             dwg.Editor.WriteMessage("\n" + dwg.Name);
  33.             dwg.Editor.WriteMessage("\nInstance command executed.");
  34.             dwg.Editor.WriteMessage("\nInstance command count: {0}", _instanceCount);
  35.         }
  36.     }
  37. }


1. Start AutoCAD and "NETLOAD" to DLL;
2. With current drawing "Drawing1.dwg", enter command "InstanceCommand", the command line shows:

Command: instancecommand

**************
Constructing...
**************
----------------------------
Drawing1.dwg
Instance command executed.
Instance command count: 1

As you can see, the class' constructor is called due to the instance (non-static) command method.

3. Now, run "InstanceCommand" command again with "Drawing1.dwg". We now can guess that the constructor of the class will not run again since the class instance has already been created with this drawing (Drawing1.dwg), and the count for instance command will increment to 2. Here is the command line response, as expected:

Command: instancecommand
-------------------------------
Drawing1.dwg
Instance command executed.
Instance command count: 2

4. Let's run the static command. The command line shows:
   
Command: staticcommand
--------------------------
Drawing1.dwg
Static command executed.
Static command count: 1
   
5. Now, open a new drawing in AutoCAD: Drawing2.dwg.
6. Run the static command. we would expect the static command count to increment to 2, like this:
   
Command: staticcommand

--------------------------
Drawing2.dwg
Static command executed.
Static command count: 2

7. Run instance command in Drawing2.dwg. the command shows:
   
Command: instancecommand

**************
Constructing...
**************
----------------------------
Drawing2.dwg
Instance command executed.
Instance command count: 1


As we can see, the class' constructor is called again, because the instance command runs in drawing2.dwg the first time.
   
8. Now go back to drawing1.dwg.
9. Run static command. we surely know the static command count will be 3, in spite of drawing switching:
   
Command: staticcommand

--------------------------
Drawing1.dwg
Static command executed.
Static command count: 2

10. Run instance command in drawing1.dwg, expecting that the instance command count in drawing1.dwg will be 3 and no class' constructor is called. Here is command line shows:
   
Command: instancecommand

-------------------------------
Drawing1.dwg
Instance command executed.
Instance command count: 3

Summery:
   
If your class/class method needs to manipulate data/object accross drawings in an AutoCAD session, you use static method and the data/object can be static data/object member of the class. If your static data/object member of the class has to be initialized, you cannot do it in the class' constructor. In this case, you usually delare the static data/object to a default value/null like this:
   
private static int _count=0;
   
or
   
private static MyObject _obj=null;
   
Then, in the static command method, you do:
   
if (_count==0)
{
     //Initialize to other value if necessary
}
//then use the static data accross drawings
   
or
   
if (_obj==null)
{
     _obj=new MyObject(); //instantiate the data object if it hasn't been.
}
//the use the static object accross drawings
   
If the data/object to be manipulated by the class/class method is drawing specific, for example a drawing tracking/index data object, then you can declare the data/object as the class' data/object member (non-static), and initilize it in the class' constructor. You use instance method to manipulate the data/object
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-22 08:20 , Processed in 0.352947 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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