编码是艺术

游戏编程

博客园 首页 新随笔 联系 订阅 管理

    前些天突然感受到Visual Studio 中的属性格控件是不错的,我想把我现有的一个动态对象的属性邦定到属性格中,开始以为.Net一定很简单,结果我开始找了大量的资料,有朋友和我说TypeConverter我顺着这个路线去找,查到了反射,还有相关属性格的资料,在网上找到了仅有的几篇文章中学习到了动态对象的实现方法.觉得.Net做了太多的事情.

 

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace ActiveXProperty
{

    
public class ActXProperty
    
{
        
private string myName;
        
private object myValue;

        
public string Name
        
{
            
get 
            

                
return myName; 
            }

            
set 
            

                myName 
= value; 
            }

        }


        
public object Value
        
{
            
get
            
{
                
return myValue;
            }

            
set
            
{
                myValue 
= value;
            }

        }


        
/// <returns><see cref="T:System.String"></see>,表示当前的 <see cref="T:System.Object"></see></returns>
        public override string ToString()
        
{
            
return "Name: " + myName + ", Value: " + Value;
        }

    }


    
public class ActXPropertyDescriptor : PropertyDescriptor
    
{
        
private ActXProperty myAXP;

        
public ActXPropertyDescriptor(ActXProperty axp,Attribute[] attrs) : base(axp.Name,attrs)
        
{
            myAXP 
= axp;
        }


        
public override bool CanResetValue(object component)
        
{
            
return false;
        }


        
public override Type ComponentType
        
{
            
get return this.GetType(); }
        }


        
public override object GetValue(object component)
        
{
            
return myAXP.Value;
        }


        
public override bool IsReadOnly
        
{
            
get return false; }
        }


        
public override Type PropertyType
        
{
            
get return myAXP.Value.GetType(); }
        }


        
public override void ResetValue(object component)
        
{
            
        }


        
public override void SetValue(object component, object value)
        
{
            myAXP.Value 
= value;
        }


        
public override bool ShouldSerializeValue(object component)
        
{
            
return false;
        }

    }


    
public class ActXPropertyList : List<ActXProperty>, ICustomTypeDescriptor
    
{
        
ICustomTypeDescriptor Members

        
/// <returns><see cref="T:System.String"></see>,表示当前的 <see cref="T:System.Object"></see></returns>
        public override string ToString()
        
{
            StringBuilder sb 
= new StringBuilder();
            
for(int i=0; i<Count; ++i)
            
{
                sb.Append(
"[" + i + "]" + this[i].ToString() + "\n");
            }

            
return sb.ToString();
        }

    }



}


ActXPropertyList axpl = new ActXPropertyList();
ActXProperty axpA = new ActXProperty();
axpA.Name = "haha";
axpA.Value = 1;
ActXProperty axpB = new ActXProperty();
axpB.Name = "xixi";
axpB.Value = "hello";
axpl.Add(axpA);
axpl.Add(axpB);

propertyGrid1.SelectObject = axpl;

这样就可以自由加入多少个属性了, 这只是一个初步,还将继续研究如何自定义类型编辑方法,数据更新等.
posted on 2007-01-18 13:20  宋良  阅读(588)  评论(0编辑  收藏  举报