0

as all of you know that there is new concept in C# 3.5 i.e. Anonymous type. how to create an anonymous type which implements one interface. Actually while writing UT for my component and also to improve the line coverage,i need this.

Kindly help me on this.

user238169
  • 11
  • 1
  • 1
    Anonymouse types cannot implement interfaces. What is it that you're trying to achieve? What is the underlying requirement? – Aviad P. Dec 29 '09 at 09:09

3 Answers3

4

C# doesn't support creating anonymous types that implement an interface. From MSDN:

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

You should probably define a new concrete type instead of using an anonymous type so that you can implement the interface you need.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

It sounds like you are looking for a mocking framework like MOQ: http://code.google.com/p/moq/

It doesn't leverage Anonymous Types - these are very different java's version

Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57
1

It is not possible to have an annonymous type implement an interface. Since annonymous types are data only, this looks like you want to implement a data-only interface to me. I do consider this a bad design. You should rather create a DTO (Data-Transfer-Object, a class with just data).

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

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172