简体   繁体   中英

'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred

I'm dealing with this exception, I read through the internet solutions but nothing helped. My code so far.

ServiceReference.CompositeType egk = new ServiceReference.CompositeType();
                egk = client.getStatus();
                appendToList(egk);

This method adds the obj to the list. At this part i receive the exception. The issue is at the parenthesis, the dynamic composite is causing the problems but I need it to change dynamic and I also know that the composite obj has the parameters.

public static void appendToList(dynamic composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("obj is null");
        }
        else
        {
            var response = myList.Find(r => r.mName == composite.mName);
            if (response == null)
            {
                myList.Add(composite);
            }
            else
            {
                int n = 0;
                foreach (CompositeType item in myList)
                {
                    if (item.mName == composite.mName)
                    {
                        n = myList.IndexOf(item);
                        myList[n] = composite;
                        break;
                    }
                }
            }
        }
    }

My full exception:

Status: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'System.Collections.Generic.List.Add(eserv.CompositeType)' has some invalid arguments

Try casting when adding to list, like; (if the list type is your own custom type)

  myList.Add(Convert.ChangeType(composite,typeof(CompositeType)));

or

   myList.Add((CompositeType)composite);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM