using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace DynamicFactory { class Program { static void Main(string[] args) { string choiceString; int choice; string chosenClass; bool foundInList = false; //could be populated from database or directory //Used just to display options to user Dictionary objectsToList = new Dictionary(); //Add different types which could be created objectsToList.Add(1, "CardPayment"); objectsToList.Add(2, "CashPayment"); objectsToList.Add(3, "OnlinePayment"); Console.WriteLine("Dynamic Factory Test Console"); Console.WriteLine("IPayment"); Console.WriteLine(""); //Print them out to user foreach (KeyValuePair d in objectsToList) { Console.WriteLine(d.Key + "\t" + d.Value); } Console.WriteLine(""); //This gets all the types in the current assembly and prints them out //Assembly assembly = Assembly.GetExecutingAssembly(); //foreach (Type t in assembly.GetTypes()) // Console.WriteLine(t.Name); while (true) { Console.Write("Please make your choice of which object to create: "); choiceString = Console.ReadLine(); choice = Convert.ToInt32(choiceString); //Get the name of the number the user entered foundInList = objectsToList.TryGetValue(choice, out chosenClass); //make sure it is in the list, otherwise warn the user. Exit is Control-C if (foundInList) { //Create a Factory object with the name Factory f = new Factory(chosenClass); //Get the object created IPayment payment = f.GetPaymentObject(); //Write out the name of the object Console.WriteLine(payment.ObjectType); //Could have used the overload static method IPayment payment2 = Factory.GetPaymentObject(chosenClass); //Write out the name of the object Console.WriteLine(payment2.ObjectType); } else { Console.WriteLine("Could not find entry"); } } } } class Factory { string className; public Factory(string cls) { this.className = cls; } public IPayment GetPaymentObject() { //Get the current assembly object Assembly assembly = Assembly.GetExecutingAssembly(); //Get the name of the assembly (this will include the public token and version number AssemblyName assemblyName = assembly.GetName(); //Use just the name concat to the class chosen to get the type of the object Type t = assembly.GetType(assemblyName.Name + "." + className); //Create the object, cast it and return it to the caller return (IPayment)Activator.CreateInstance(t); } public static IPayment GetPaymentObject(string className) { //Same code as above, but className comes from parameter Assembly assembly = Assembly.GetExecutingAssembly(); AssemblyName assemblyName = assembly.GetName(); Type t = assembly.GetType(assemblyName.Name + "." + className); return (IPayment)Activator.CreateInstance(t); } } interface IPayment { string ObjectType {get;} } class CardPayment : IPayment { public CardPayment() { } public string ObjectType { get { return this.GetType().FullName; } } } class CashPayment : IPayment { public CashPayment() { } public string ObjectType { get { return this.ToString(); } } } class OnlinePayment : IPayment { public OnlinePayment() { } public string ObjectType { get { return this.ToString(); } } } }