14

It is possible, given only an interface, to create an object from this?

Something like:

var obj = new IWidget();

(I know this code isn't right - VS stays cannot create an instance of IWidget)

I'm in a context where my project has references to the interfaces, and I want to create concrete objects and return them from a method - but I can't figure out how to create the objects purely from the interfaces.

wows
  • 10,687
  • 7
  • 27
  • 27
  • yes, but you need to specify a COM CoClass: http://blogs.msdn.com/brada/archive/2004/10/11/241067.aspx – Mark Synowiec Feb 02 '10 at 19:22
  • @Mark - Where did you get the COM from? – Orion Edwards Feb 02 '10 at 19:56
  • @Orion - Are you asking why I said COM CoClass instead of just CoClass? It's an attribute related to COM interop. ie you cant use the CoClass attribute without the ComImport attribute. – Mark Synowiec Feb 02 '10 at 20:50
  • @Mark - No, I'm familiar with COM interop - I asked why, because there was no mention in the question whatsoever about any COM, and COM interop is a comparatively rare edge case in C#. Scrolling down, I found jon skeet mention that the code is correct for COM objects, so presumably that's what you were talking about? – Orion Edwards Feb 03 '10 at 19:30
  • @Orion - Gotcha, yeah, mainly just wanted to point out with COM attributes specified it's possible, otherwise it isnt. – Mark Synowiec Feb 04 '10 at 12:14

5 Answers5

13

You can't create an object from an interface. You can create an object from a class that uses that interface.

For example:

IList<string> x = new IList<string>();

will not work.

IList<string> x = new List<string>();

will.

Interfaces cannot be created, only objects that use the interface can be created.

taylonr
  • 10,732
  • 5
  • 37
  • 66
10

That code is actually correct for COM objects, due to compiler wizardry. Normally it's not though.

The problem is, how is the compiler meant to know which implementation to create? There could be any number around. You might want to consider having a factory for the interface, or possibly using dependency injection (e.g. with Spring.NET or Castle Windsor).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • For a complete list of DI containers see this SO question http://stackoverflow.com/questions/148908/which-dependency-injection-tool-should-i-use or this list http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx – smaclell Feb 02 '10 at 19:24
4

This will not be possible purely from the interfaces (and shouldn't be, what if there is more than one implementation of it?). It sounds like what you want to do is expose an interface, but not the implementation. Is that correct?

You essentially would want a Factory Pattern. This pattern involves making a method that returns the interface, but internally instantiates a concrete type. It lets you hide the concrete type from anyone using the interface.

If you go a step further you could use Inversion of Control (IoC). I don't know what the best option is for doing this in .Net, but one option is Spring.Net. You use a configuration file to define all of the different setups for your concrete objects, and then have spring automatically "inject" those instances into your classes that use the interface.

Chris Pitman
  • 12,990
  • 3
  • 41
  • 56
0

No would be the short answer. But I guess you could use an IoC container to inject an implemtation.

chrissie1
  • 5,014
  • 3
  • 26
  • 26
0

You might be looking for a Dependency Injection framework.

oefe
  • 19,298
  • 7
  • 47
  • 66