读书人

怎么获得私有对象成员

发布时间: 2012-01-05 22:36:54 作者: rapoo

如何获得私有对象成员?
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: testKey1Value: testValue1
Key: testKey2Value: testValue2
Key: keyToDeleteValue: valueToDelete
Key: testKey3Value: testValue3
[解决办法]
TO:如果用了数组的话,就用不了面向对象

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

没太明白..

读书人网 >C#

热点推荐