C# 从字典到字符串

C# 从字典到字符串,c#,extension-methods,idictionary,C#,Extension Methods,Idictionary,我创建了一个IDictionary扩展来将IDictionary Exception.Data值写入字符串 分机代码: public static class DictionaryExtension { public static string ToString<TKey, TValue>(this IDictionary<TKey, TValue> source, string keyValueSeparator, string sequenceSeparato

我创建了一个IDictionary扩展来将IDictionary Exception.Data值写入字符串

分机代码:

public static class DictionaryExtension
{
    public static string ToString<TKey, TValue>(this IDictionary<TKey, TValue> source, string keyValueSeparator, string sequenceSeparator)
    {
        if (source == null)
            throw new ArgumentException("Parameter source can not be null.");

        return source.Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.Key + keyValueSeparator + x.Value + sequenceSeparator), sb => sb.ToString(0, sb.Length - 1));           
    }
}
当我在Exception.Data.ToString=,上使用这个扩展时,我得到一个错误

无法从用法推断类型参数

知道如何解决这个问题吗?

属于IDictionary类型,而不是IDictionary类型

您需要将扩展方法更改为:

public static string ToString(this IDictionary source, string keyValueSeparator,
                                                       string sequenceSeparator) 
{ 
    if (source == null) 
        throw new ArgumentException("Parameter source can not be null."); 

    return source.Cast<DictionaryEntry>()
                 .Aggregate(new StringBuilder(),
                            (sb, x) => sb.Append(x.Key + keyValueSeparator + x.Value
                                                  + sequenceSeparator),
                            sb => sb.ToString(0, sb.Length - 1));            
} 
是IDictionary类型,而不是IDictionary类型

您需要将扩展方法更改为:

public static string ToString(this IDictionary source, string keyValueSeparator,
                                                       string sequenceSeparator) 
{ 
    if (source == null) 
        throw new ArgumentException("Parameter source can not be null."); 

    return source.Cast<DictionaryEntry>()
                 .Aggregate(new StringBuilder(),
                            (sb, x) => sb.Append(x.Key + keyValueSeparator + x.Value
                                                  + sequenceSeparator),
                            sb => sb.ToString(0, sb.Length - 1));            
} 

例外情况是,您缺少一个强制类型转换。 我在测试项目中复制了您的代码,但无法重现您的错误。 尝试使用x.Key.ToString和x.Value.ToString。 我只发现字典为空时出现错误:
sb.ToString0,sb.Length-1当lenght为零时不起作用

异常是指您缺少一个强制转换。 我在测试项目中复制了您的代码,但无法重现您的错误。 尝试使用x.Key.ToString和x.Value.ToString。 我只发现字典为空时出现错误:
sb.ToString0,sb.Length-1当lenght为零时不起作用

问题可能与Exception.Data是IDictionary而不是IDictionary这一事实有关。我很惊讶它竟然调用了扩展方法。@ThomSmith:它没有调用扩展方法。编译器错误发生在Exception.Data.ToString=,|检查[Convert object HtmlatAttributes to string][1][1]:问题可能与Exception.Data是IDictionary而不是IDictionary这一事实有关。我很惊讶它竟然调用了扩展方法。@ThomSmith:它没有调用扩展方法。编译器错误发生在Exception.Data.ToString=,|检查[Convert object HtmlatAttributes to string][1][1]:行上,然后在源代码上得到相同的异常。Aggregate@Tomas:这是您遇到的编译器错误,不是异常。异常发生在运行时。无论如何,请查看更新。最重要的部分是演员阵容。然后我在源代码上得到了同样的异常。Aggregate@Tomas:这是您遇到的编译器错误,不是异常。异常发生在运行时。无论如何,请查看更新。重要的部分是演员阵容。