本文介绍了'System.Collections.Generic.IList<对象>'新新旗新新新旗新新旗旗新新旗旗新新旗新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有一个类,Foo,看起来像这样:

  public class Foo:IFoo 
{
public Foo()
{
Bars = new List< dynamic>();
}
public IList< dynamic>酒吧{get;组; }
}

界面IFoo看起来像:

  public interface IFoo 
{
IList< dynamic>酒吧{get;组;
}

现在,当我执行以下操作:

  IFoo foo = new Foo(); 
dynamic a = new System.Dynamic.ExpandoObject();
a.Prop = 10000;
dynamic b = new System.Dynamic.ExpandoObject();
b.Prop =Some Text;
foo.Bars.Add(a); //抛出一个'System.Collections.Generic.IList< object>'不包含'Add'的定义 - 异常!!!!!
foo.Bars.Add(b); // 同样在这里!!!!!

我在这里做错什么

解决方案

这是一个已知的动态绑定。



以下是一些工作。



使用 ICollection< dynamic> 替代:

  void Main()
{
IFoo foo = new Foo();
dynamic a = new System.Dynamic.ExpandoObject();
a.Prop = 10000;
dynamic b = new System.Dynamic.ExpandoObject();
b.Prop =Some Text;
foo.Bars.Add(a);
foo.Bars.Add(b);
}

public interface IFoo
{
ICollection< dynamic>酒吧{get;组;


public class Foo:IFoo
{
public Foo()
{
Bars = new List< dynamic>() ;
}

public ICollection< dynamic>酒吧{get;组; }
}

或直线列表<动态> ;

  public interface IFoo 
{
列表< ;动态>酒吧{get;组;


public class Foo:IFoo
{
public Foo()
{
Bars = new List< dynamic>() ;
}

public List< dynamic>酒吧{get;组;
}

或使用 dynamic foo

  void Main()
{
dynamic foo = new Foo();
dynamic a = new System.Dynamic.ExpandoObject();
a.Prop = 10000;
dynamic b = new System.Dynamic.ExpandoObject();
b.Prop =Some Text;
foo.Bars.Add(a);
foo.Bars.Add(b);
}

或不动态绑定 add ,通过转换为对象

  void Main()
{
IFoo foo = new Foo();
dynamic a = new System.Dynamic.ExpandoObject();
a.Prop = 10000;
dynamic b = new System.Dynamic.ExpandoObject();
b.Prop =Some Text;
foo.Bars.Add((object)a);
foo.Bars.Add((object)b);
}

或使用第三方框架(如即兴即时界面)更具表现力& (在nuget中)

  void Main()
{
dynamic New = Builder.New< ExpandoObject>();

IFoo foo = Impromptu.ActLike(
New.Foo(
Bars:New.List(
New.Obj(Prop:10000),
New.Obj(Prop:Some Text)


);
}

public interface IFoo
{
IList< dynamic>酒吧{get;组; }
}


Say that I have a class, Foo, looking something like this:

public class Foo : IFoo
{
    public Foo()
    {
        Bars = new List<dynamic>();
    }
    public IList<dynamic> Bars { get; set; }
}

The interface IFoo looks like:

public interface IFoo
{
    IList<dynamic> Bars { get; set; }
}

Now, when I do the following:

IFoo foo = new Foo();
dynamic a = new System.Dynamic.ExpandoObject();
a.Prop = 10000;
dynamic b = new System.Dynamic.ExpandoObject();
b.Prop = "Some Text";
foo.Bars.Add(a); // Throws an 'System.Collections.Generic.IList<object>' does not contain a definition for 'Add' - exception!!!!!
foo.Bars.Add(b); // Same here!!!!!

What am I doing wrong here?????

解决方案

This is a known dynamic binding issue.

Here are some work arounds.

Use ICollection<dynamic> instead:

void Main()
{
    IFoo foo = new Foo();
    dynamic a = new System.Dynamic.ExpandoObject();
    a.Prop = 10000;
    dynamic b = new System.Dynamic.ExpandoObject();
    b.Prop = "Some Text";
    foo.Bars.Add(a);
    foo.Bars.Add(b); 
}

public interface IFoo
{
    ICollection<dynamic> Bars { get; set; }
}

public class Foo : IFoo
{
    public Foo()
    {
        Bars = new List<dynamic>();
    }

    public ICollection<dynamic> Bars { get; set; }
}

Or straight up List<dynamic>:

public interface IFoo
{
    List<dynamic> Bars { get; set; }
}

public class Foo : IFoo
{
    public Foo()
    {
        Bars = new List<dynamic>();
    }

    public List<dynamic> Bars { get; set; }
}

Or use dynamic foo:

void Main()
{
    dynamic foo = new Foo();
    dynamic a = new System.Dynamic.ExpandoObject();
    a.Prop = 10000;
    dynamic b = new System.Dynamic.ExpandoObject();
    b.Prop = "Some Text";
    foo.Bars.Add(a);
    foo.Bars.Add(b); 
}

Or don't dynamic bind add, by casting to object:

void Main()
{
    IFoo foo = new Foo();
    dynamic a = new System.Dynamic.ExpandoObject();
    a.Prop = 10000;
    dynamic b = new System.Dynamic.ExpandoObject();
    b.Prop = "Some Text";
    foo.Bars.Add((object)a); 
    foo.Bars.Add((object)b); 
}

Or be more expressive using a third party framework like my impromptu interface with ActLike & Prototype Builder Syntax (in nuget).

void Main()
{
    dynamic New = Builder.New<ExpandoObject>();

    IFoo foo = Impromptu.ActLike(
                   New.Foo(
                       Bars: New.List(
                                 New.Obj(Prop:10000),
                                 New.Obj(Prop:"Some Text")
                             )
                       )
                   );
}

public interface IFoo
{
    IList<dynamic> Bars { get; set; }
}

这篇关于'System.Collections.Generic.IList&LT;对象&gt;'新新旗新新新旗新新旗旗新新旗旗新新旗新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:10