.NET组件编程(4) 用户自定义Property Editor的调试

        园子里的高人太多了,第2章遗留下来的Property DefaultValueAttribute问题解决掉了,感谢Colin Han的帮助,我对DefaultValueAttribute的理解有点误解了,msdn中对DefaultValueAttribute的说明为: “可视化设计器可以使用默认值重置成员的值。代码生成器也可使用默认值确定是否为成员生成代码”,我把其理解为了创建Component时,对标示有DefaultValueAttribute的Property产生默认值,Colin Han的解释为:“这个特性可以帮助IDE减少Code生成的工作,如果设计时某个标示有DefaultValueAttribute的Property的值和DefaultValue的值一样,IDE将不会为这个属性生成代码;否则,IDE会自动在InitializeComponent中添加的代码”
举例说明:

Customer Component有个Age Property,声明如下

        [Description("Customer's Age"), DefaultValue(20)]
        
public int Age
        
{
            
get return _age; }
            
set { _age = value; }
        }

有个窗体Form1,在Form1上贴有一个Customer类型的customer1,在设计时对Age设值为20,Form1的InitializeComponent()为

        private void InitializeComponent()
        
{
            
this.customer1 = new Components.Customer();
            
// 
            
// customer1
            
// 
            this.customer1.Address = null;
            
this.customer1.CreateTime = new System.DateTime(((long)(0)));
            
this.customer1.Id = null;
            
this.customer1.Sex = null;
        }

如果在设计时对Age设值为40,Form1的InitializeComponent()为

        private void InitializeComponent()
        
{
            
this.customer1 = new Components.Customer();
            
// 
            
// customer1
            
// 
            this.customer1.Address = null;
            
this.customer1.Age = 40;
            
this.customer1.CreateTime = new System.DateTime(((long)(0)));
            
this.customer1.Id = null;
            
this.customer1.Sex = null;
        }

 

        第二种情况比第一种多了句this.customer1.Age = 40;
    
        
        本来这章准备讲Component Designer的,但是在第3章的评论中chnking朋友问我,用户自定义Property Editor中如何调试的问题,我原来在开发Component的过程都是用MessageBox来弹出信息,但是lichdr提出了个更好的办法,监视进程,这样就能对用户自定义的Property Editor进行调试,我在vs 6.0里用这种方法调试过,但是在.net环境上我从来我想到过,我想很多朋友在开发过程也可能会遇到这样的问题,至少我是遇到了,而且走了很多的弯路,所以把它作为单独的一章进行讲解下,在这里再次谢谢lichdr。
        下面我用第三章的源代码进行演示下:源代码下载
        先打开一个vs 2005的IDE(IDE1),在IDE1中打开WindowsApplication9解决方案,在Components.Demo3.cs中设置如下断点
        
        再打开一个vs 2005的IDE(IDE2),在IDE1做如下的操作:

        在IDE2中打开WindowsApplication9解决方案,并对Demo3的Grade Property设值时,IDE1中会发生断点调试中断。
       

posted @ 2006-03-08 19:06  mapserver  阅读(6069)  评论(12编辑  收藏  举报