日期:2014-05-20  浏览次数:20775 次

如何获得私有对象成员?
System.Collections.Specialized.OrderedDictionary   Dic   =   new   System.Collections.Specialized.OrderedDictionary();
                        Dic.Add( "a ",   "aaa ");
                        Dic.Add( "b ",   "bbb ");
                        Dic.Keys
:
因为Dic.Keys   类型[反射后查得]为   一个私有类System.Collections.Specialized.OrderedDictionary.OrderedDictionaryKeyValueCollection
求   获得keys列表的方法

------解决方案--------------------
什么是私有类?
Keys是OrderedDictionary的公共属性,ICollection接口类型的

------解决方案--------------------
我剛查過msdn2005,裡面的keys是public的。
------解决方案--------------------
public ICollection Keys { get; }
获取 ICollection 对象,该对象包含 OrderedDictionary 集合中的键。
------解决方案--------------------
TO:因为Dic.Keys 类型[反射后查得]为 一个私有类System.Collections.Specialized.OrderedDictionary.OrderedDictionaryKeyValueCollection
求 获得keys列表的方法

不就直接Dic.Keys就可以了吗?

它是公共的属性...
------解决方案--------------------
参见MSDN:

ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref2/html/P_System_Collections_Specialized_OrderedDictionary_Keys.htm

example like :

// Creates and initializes a OrderedDictionary.
OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add( "testKey1 ", "testValue1 ");
myOrderedDictionary.Add( "testKey2 ", "testValue2 ");
myOrderedDictionary.Add( "keyToDelete ", "valueToDelete ");
myOrderedDictionary.Add( "testKey3 ", "testValue3 ");

ICollection keyCollection = myOrderedDictionary.Keys;
ICollection valueCollection = myOrderedDictionary.Values;

// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);
------解决方案--------------------
try..

OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add( "testKey1 ", "testValue1 ");
myOrderedDictionary.Add( "testKey2 ", "testValue2 ");
myOrderedDictionary.Add( "keyToDelete ", "valueToDelete ");
myOrderedDictionary.Add( "testKey3 ", "testValue3 ");
string[] mykeys = new string[myOrderedDictionary.Count];
string[] myvalues = new string[myOrderedDictionary.Count];
myOrderedDictionary.Keys.CopyTo(mykeys, 0);
myOrderedDictionary.Values.CopyTo(myvalues, 0);
for (int i = 0; i < myOrderedDictionary.Count; i++)
{
Console.WriteLine( "Key: " + mykeys[i] + "\tValue: " + myvalues[i]);
}

输出:
Key: testKey1 Value: testValue1
Key: testKey2 Value: testValue2
Key: keyToDelete Value: valueToDelete
Key: testKey3 Value: testValue3
------解决方案--------------------
TO:如果用了数组的话,就用不了面向对象

用数组与面向对象有什么关系吗?

没太明白..