Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anonymous class implement interface?

Is it possible to have an anonymous type implement an interface?

I've got a piece of code that I would like to work, but don't know how to do this.

I've had a couple of answers that either say no, or create a class that implements the interface construct new instances of that. This isn't really ideal, but I'm wondering if there is a mechanism to create a thin dynamic class on top of an interface which would make this simple.

public interface DummyInterface {     string A { get; }     string B { get; } }  public class DummySource {     public string A { get; set; }     public string C { get; set; }     public string D { get; set; } }  public class Test {     public void WillThisWork()     {         var source = new DummySource[0];         var values = from value in source                      select new                      {                          A = value.A,                          B = value.C + "_" + value.D                      };          DoSomethingWithDummyInterface(values);      }      public void DoSomethingWithDummyInterface(IEnumerable<DummyInterface> values)     {         foreach (var value in values)         {             Console.WriteLine("A = '{0}', B = '{1}'", value.A, value.B);         }     } } 

I've found an article Dynamic interface wrapping that describes one approach. Is this the best way of doing this?

like image 364
Nick Randell Avatar asked Oct 10 '08 12:10

Nick Randell


People also ask

Can an anonymous class implement an interface in Java?

The syntax of anonymous classes does not allow us to make them implement multiple interfaces.

Can an anonymous class be declared as implementing an interface?

A normal class can implement any number of interfaces but the anonymous inner class can implement only one interface at a time. A regular class can extend a class and implement any number of interfaces simultaneously. But anonymous Inner class can extend a class or can implement an interface but not both at a time.

How many interfaces can an anonymous class implement?

We can implement multiple numbers of interfaces by using normal class but, with an anonymous inner class, we can only implement one interface.

Can Anonymous classes implement an interface a yes b no?

Explanation: Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous class extends that one class).


2 Answers

No, anonymous types cannot implement an interface. From the C# programming guide:

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.

like image 71
HasaniH Avatar answered Sep 22 '22 03:09

HasaniH


While the answers in the thread are all true enough, I cannot resist the urge to tell you that it in fact is possible to have an anonymous class implement an interface, even though it takes a bit of creative cheating to get there.

Back in 2008 I was writing a custom LINQ provider for my then employer, and at one point I needed to be able to tell "my" anonymous classes from other anonymous ones, which meant having them implement an interface that I could use to type check them. The way we solved it was by using aspects (we used PostSharp), to add the interface implementation directly in the IL. So, in fact, letting anonymous classes implement interfaces is doable, you just need to bend the rules slightly to get there.

like image 25
Mia Clarke Avatar answered Sep 21 '22 03:09

Mia Clarke