How can I use collection initializer syntax with ExpandoObject?

后端 未结 6 988
温柔的废话
温柔的废话 2021-02-07 08:24

I\'ve noticed that the new ExpandoObject implements IDictionary which has the requisite IEnumerable

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-07 08:48

    The language specification (7.5.10.3 on Collection Initializers) is a bit vague on this point as far as I can tell. It says

    For each specified element in order, the collection initializer invokes an Add method on the target object with the expression list of the element initializer as argument list, applying normal overload resolution for each invocation. Thus, the collection object must contain an applicable Add method for each element initializer.

    Unfortunately the text doesn't go into details about what an applicable Add method is, but it seems that explicitly implemented interface methods don't fit the bill as they are essentially considered private (see 13.4.1):

    It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

    ...

    Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, they are in a sense private. However, since they can be accessed through an interface instance, they are in a sense also public.

提交回复
热议问题