无法将 lambda 表达式转换为类型“System.Collections.Generic.IEqualityComp
本文关键字:System Collections Generic IEqualityComp 类型 lambda 表达式 转换 | 更新日期: 2024-10-30 18:56:01
方法签名:
public static IDictionary<string, object> ListX(this object instance)
法典:
if (instance == null)
throw new NullReferenceException();
var result = instance as IDictionary<string, object>;
if (result != null)
return result;
return instance.GetType()
.GetProperties()
.ToDictionary(x => x.Name, x =>
{
object value = x.GetValue(instance);
if (value != null)
{
var valueType = value.GetType();
// Whe should manually check for string type because IsPrimitive will return false in case of string
if (valueType.IsPrimitive || valueType == typeof(string))
{
return value;
}
// If the value type is enumerable then we iterate over and recursively call ToDictionary on each item
else if (valueType.GetInterfaces().Any(t => t == typeof(IEnumerable)))
{
List<object> elements = new List<object>();
foreach (var item in value as IEnumerable)
{
elements.Add(item.ToDictionary());
}
return elements;
}
// The value type is a complex type so we recursively call ToDictionary
else
{
return value.ToDictionary();
}
}
return null;
});
x
我得到Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer<string>' because it is not a delegate type
.
这里的道具是什么?
好的,我更新了该代码。
问题是ToDictionary期望IEqualityComparer作为第二个参数,而您提供的内容并未实现该接口。
您可以在以下链接中获得有关如何使用此 metod 的说明:http://www.dotnetperls.com/todictionary