开发者

C# Cannot convert lambda expression to type 'dynamic' because it is not a delegate type

开发者 https://www.devze.com 2023-03-02 04:51 出处:网络
Suppose I have a List<dynamic> myList = new List<dynamic>(); Inside a class: public class DynamicMixin : DynamicObject

Suppose I have a

List<dynamic> myList = new List<dynamic>();

Inside a class:

public class DynamicMixin : DynamicObject
{
    internal List<dynamic> myList= new List<dynamic>();

    public void AddInterface<T>(T _item) where T:class{
        Interfaces.Add(_item);
    }

    public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
    {
        if (myList.Contains((item)=>item.GetType().Equals(indexes[0].GetType())){
            /* do something */
        }
        return base.TryGetIndex(binder, indexes, out result);
    }

}

I'm trying to write myDynamicObject[typeof(IDisposable)] So I would get the IDiposable object which belongs to myDynamicObject.

This line gives me an error:

if (myList.Contains((item)=>item.GetType().Equals(indexes[0].开发者_如何学JAVAGetType())){

Cannot convert lambda expression to type 'dynamic' because it is not a delegate type

I'm able to do it by iterating through the list: But why I'm not capable of using Contains ?


Contains() expects an actual item (of type dynamic in your case) not a delegate, I think you want Any() :

 if (myList.Any( item => item.GetType().Equals(indexes[0].GetType()))
 {


Becasue Contains is declared as:

public bool Contains(
    T item
)

You shoud use Any(your lambda)


IEnumerable<T>.Contains() does not have an overload that takes a lambda.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号