To iterate over a System.Object that is a Dictionary in runtime using a foreach loop, you need to cast the System.Object to the specific Dictionary type and then iterate over the key-value pairs.

Here's an example of how you can achieve this:

System.Object collection;

var collectionType = collection.GetType();

if (collectionType.IsGenericType && collectionType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) {
    var dictionary = (IDictionary)collection;
    
    foreach (DictionaryEntry item in dictionary) {
        var key = item.Key;
        var value = item.Value;
        
        // Process key-value pair here
    }
}

In this example, we first check if the collection is of a generic type and if it matches the Dictionary type. Then, we cast the collection to an IDictionary interface, which allows us to iterate over the key-value pairs using a foreach loop. Inside the loop, you can access the key and value of each pair and perform any required processing.

Iterating over a Dictionary Object in a Foreach Loop at Runtime

原文地址: http://www.cveoy.top/t/topic/qaqh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录