I had the same issue recently, how do I implement a factory pattern in C#, having the consumer classes of the factory, the factory itself, and the classes it creates all residing in the same assembly but not allowing the consumer classes to create any of the classes that will be created through the factory. Well there is no direct scope level that allows this. The solution would be to create internals in a different assembly but that's not what I wanted. So this is what I did:
1) define the common factory interface as public 2) define the factory class as public with the factory method that returns an interface defined in (1) 3) define all classes that implement the interface in (1) as private, nested classes inside the factory class Below was my test console class to get it working: using System; namespace ConsoleApplication3 { public interface IInterface { void DoSomething(); } public class FactoryClass { public static IInterface CreateSomething(string someInfo) { switch (someInfo) { case "xyz": return new SomeClass(); break; default: return null; break; } } // Factory Classes private class SomeClass : IInterface { public void DoSomething() { } } } /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { IInterface i = FactoryClass.CreateSomething("xyz"); i.DoSomething(); // compiler error: 'FactoryClass.SomeClass' is inaccessible due to its protection level // FactoryClass.SomeClass s = new FactoryClass.SomeClass(); } } } > -----Original Message----- > From: Michael Potter [SMTP:[EMAIL PROTECTED]] > Sent: Wednesday, July 31, 2002 9:11 PM > To: [EMAIL PROTECTED] > Subject: [ADVANCED-DOTNET] Need a C# pattern that returns an Interface > > I have a static method that will return an object of > IMyInterface. It will create the best 1 of many > mutually exlusive objects that implement IMyInterface. > The object created depends upon the available > hardware resources. > > The objects should not be created outside of the one > static function. How do I hide the constructor of the > objects (that implement IMyInterface) from the rest of > the world and yet, allow them to be exposed to the one > static creation method? > > Any ideas? > Mike P > > > > __________________________________________________ > Do You Yahoo!? > Yahoo! Health - Feel better, live better > http://health.yahoo.com > > You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced >DOTNET, or > subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.