Problem
Is it feasible to implement an interface with an anonymous type?
I have a piece of code that I’d like to work on, but I’m not sure how.
I’ve had a few of responses that either say no or suggest that I create a class that implements the interface and use it to make new instances of it. This isn’t ideal, but I’m wondering if there’s a way to create a thin dynamic class on top of an interface that makes it easier.
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);
}
}
}
One solution is described in the article Dynamic interface wrapping. Is this the most efficient method?
Asked by Nick Randell
Solution #1
Anonymous types, on the other hand, cannot implement an interface. The following is taken from the C# programming guide:
Answered by HasaniH
Solution #2
While the answers in the thread are all correct, I can’t help but feel compelled to remind you that it is possible to have an anonymous class implement an interface, albeit with some creative cheating.
I was creating a custom LINQ provider for my former employer in 2008, and at one point I needed to be able to distinguish “my” anonymous classes from other anonymous classes, which required them to implement an interface that I could use to type check them. We solved the problem by leveraging aspects (we used PostSharp) to directly implement the interface in the IL. In truth, allowing anonymous classes to implement interfaces is possible; all you have to do is bend the rules slightly.
Answered by Mia Clarke
Solution #3
I’ve wanted to cast anonymous types to interfaces for a long time, however the existing implementation requires you to have an implementation of that interface.
The best solution around it is having some type of dynamic proxy that creates the implementation for you. You can use the fantastic LinFu project to replace
select new
{
A = value.A,
B = value.C + "_" + value.D
};
with
select new DynamicObject(new
{
A = value.A,
B = value.C + "_" + value.D
}).CreateDuck<DummyInterface>();
Answered by Arne Claassen
Solution #4
A dynamic proxy can be used to implement interfaces for anonymous types.
To handle this scenario, I created a GitHub extension method and a blog article at http://wblo.gs/feE.
The following is an example of how to apply the method:
class Program
{
static void Main(string[] args)
{
var developer = new { Name = "Jason Bowers" };
PrintDeveloperName(developer.DuckCast<IDeveloper>());
Console.ReadKey();
}
private static void PrintDeveloperName(IDeveloper developer)
{
Console.WriteLine(developer.Name);
}
}
public interface IDeveloper
{
string Name { get; }
}
Answered by Jason Bowers
Solution #5
No, you can’t have an anonymous type perform anything other than have a few properties. You’ll have to invent your own type. I didn’t read the linked article very carefully, but it appears to employ Reflection. You can use Emit to build new types on the fly, but you won’t be able to do what you want if you only talk about C#.
Answered by Marc Gravell
Post is based on https://stackoverflow.com/questions/191013/can-anonymous-class-implement-interface