定义两个类,第二个类中有第一个类的一个arrlylist
public class Curve
{
public Curve()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private float max_value = 0F;
private float min_value = 0F;
private string curve_name = "";
private Color curve_color = System.Drawing.Color.Black;
public float MaxValue
{
get
{
return max_value;
}
set
{
max_value = value;
}
}
public float MinValue
{
get
{
return min_value;
}
set
{
min_value = value;
}
}
public string CurveName
{
get
{
return curve_name;
}
set
{
curve_name = value;
}
}
public Color CurveColor
{
get
{
return curve_color;
}
set
{
curve_color = value;
}
}
}
public class Dao
{
private int left = 0;
private int right = 0;
private Color color;
ArrayList curve = new ArrayList();
public int Left
{
get
{
return left;
}
set
{
left = value;
}
}
public int Right
{
get
{
return right;
}
set
{
right = value;
}
}
/// <summary>
/// 道的颜色
/// </summary>
public Color DaoColor
{
get
{
return color;
}
set
{
color = value;
}
}
public ArrayList Curve
{
get
{
return curve;
}
set
{
curve = value;
}
}
}
-----------------------------------
在而上调用的时候:
private void button3_Click(object sender, System.EventArgs e)
{
Dao dao = new Dao();
propertyGrid1.SelectedObject = dao;
}
----------------------------------
在属性Curve里出现了,如我所愿,但是打开集合后,里面的对象添加全是System.object类型,而非Curve类型,我也知道上面没有任何地方说明这个集合应该是curve类型,但是我不知道在哪里指定,请大侠明示。
谢谢
用ArrayList是不行的,如果你要限定一个集合添加的类型,那么你可以自己写一个,最简单的就是封装一个ArrayList在内部,如:
public class CurveCollection : ICollection,IEnumerable
{
private ArrayList _itemList;
public CurveCollection()
{
_itemList = ArrayList.Synchronized( new ArrayList() );
}
public Items this[int index]
{
get{ return (Curve)_itemList[index]; }
}
public int Count
{
get{ return _itemList.Count; }
}
public bool IsSynchronized
{
get{ return _itemList.IsSynchronized; }
}
public object SyncRoot
{
get{ return _itemList.SyncRoot; }
}
public void CopyTo( Array array,int index )
{
_itemList.CopyTo( array,index );
}
public IEnumerator GetEnumerator()
{
return _itemList.GetEnumerator();
}
public void Add( Curve item )
{
_itemList.Add( item );
}
public void Remove( Curve item )
{
_itemList.Remove( item );
}
public void RemoveAt( int index )
{
_itemList.RemoveAt( index );
}
public void RemoveRange( int index,int count )
{
_itemList.RemoveRange( index,count );
}
public void Insert( Curve item,int index )
{
_itemList.Insert( index,item );
}
public Curve[] ToArray()
{
return (Curve[])_itemList.ToArray( typeof( Curve ) );
}
}