Skip to main content
 首页 » 编程设计

c#之与字典替代的协方差

2024年02月05日16Terrylee

我正在将一些类字段输出到 XML。我不知道每个字段是什么,但我检查它是 IEnumerable 还是 IDictionary,因为我需要以一种方式处理字段,以其他方式处理集合,并以另一种方式处理字典。

您可以通过最后一个方法“DictionaryToXml”看到我的问题。字典不是协变的。 换句话说 IDictionary<object, object> dict = new IDictionary<string, string>();会抛出异常

如何做到这一点,有什么解决方法吗?

        public static string ClassToXml(object o) 
    { 
        string observeable; 
        observeable = o.GetType().ToString(); 
        XElement root = new XElement(observeable); 
 
        FieldInfo[] fields = o.GetType().GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); 
        foreach (FieldInfo f in fields) 
        { 
            try 
            { 
                string nam = f.Name; 
                if (f.FieldType.GetInterface("IDictionary") != null) 
                { 
                    XElement child = DictionaryToXml(o, f); 
                    root.Add(child); 
                } 
                else if (f.FieldType.GetInterface("IEnumerable") != null && f.FieldType != typeof(string)) 
                { 
                    XElement child = EnumerableToXml(o, f); 
                    root.Add(child); 
                } 
                else 
                { 
                    XElement child = FieldToXml(o, f); 
                    root.Add(child); 
                } 
            } 
            catch(Exception ex) { continue; } 
        } 
 
 
 
        observeable = root.ToString(); 
        return ""; 
    } 
    static XElement FieldToXml(object o, FieldInfo f) 
    { 
        string nam = f.Name; 
        object val = f.GetValue(o); 
        string stringed = val != null ? val.ToString() : ""; 
        XElement child = new XElement(nam, stringed, new XAttribute("type", "field")); 
        return child; 
    } 
    static XElement EnumerableToXml(object o, FieldInfo f) 
    { 
        string nam = f.Name; 
        XElement container = new XElement(nam, new XAttribute("type", "enumerable")); 
 
 
        IEnumerable<object> vals = (IEnumerable<object>)f.GetValue(o); 
        foreach (object obj in vals) 
        { 
            string stringed = obj.ToString(); 
 
            XElement child = new XElement("value", stringed); 
            container.Add(child); 
        } 
 
        return container; 
    } 
    static XElement DictionaryToXml(object o, FieldInfo f) 
    { 
        string nam = f.Name; 
        XElement container = new XElement(nam, new XAttribute("type", "dictionary")); 
 
        IDictionary<object, object> dict = (IDictionary<object, object>)f.GetValue(o); 
        List<KeyValuePair<object, object>> vals = dict.ToList(); 
        foreach (object obj in vals) 
        { 
            //some code 
        } 
 
        return container; 
    } 

请您参考如下方法:

您正在检查非通用 Dictionary 接口(interface),那么为什么不使用它呢?

static XElement DictionaryToXml(object o, FieldInfo f) 
{ 
    string nam = f.Name; 
    XElement container = new XElement(nam, new XAttribute("type", "dictionary")); 
 
    IDictionary dict = (IDictionary)f.GetValue(o); 
    foreach (DictionaryEntry obj in dict) 
    { 
        //some code using obj.Key and obj.Value 
    } 
 
    return container; 
} 

您可以使用更多的反射来找出您使用的是什么类型的字典,但是由于您只是试图转换为 <object, object>没有意义 - 你会得到 object来自非通用接口(interface)的键和值。

(同样,您正在检查 IEnuemrable ,但转换到 IEnumerable<object> ,这可能会误导您。)