How to create an anonymous class with interface in C #

since you all know that in C # 3.5 there is a new concept, i.e. anonymous type. how to create an anonymous type that implements one interface. In fact, when writing UT for my component, as well as to improve line coverage, I need this.

Please help me with this.

-1
source share
3 answers

C # does not support the creation of anonymous types that implement an interface. From MSDN :

Anonymous types are class types that consist of one or more public read-only types. No other kinds of class members such as methods or events are allowed. An anonymous type cannot be added to any interface or type except for an object.

, , .

+4

, , ​​ MOQ: http://code.google.com/p/moq/

- java

+2

It is not possible for a user type to implement an interface. Since anonymous types are data only, it looks like you want to implement an interface only for data. I consider this a bad design. You better create a DTO (Data-Transfer-Object, a class with just data).

However, if you really need an anonymous type to implement the interface, use duck-typing

+1
source

Source: https://habr.com/ru/post/1732569/


All Articles