Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Going more agnostic
Message
 
À
04/09/2009 13:11:22
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Divers
Thread ID:
01422695
Message ID:
01422802
Vues:
74
Hi Jim,

I would use a table to avoid a large switch statement for creating objects. Assuming you have an enum ElementType which defines the various classes and an interface IElement which is common to all objects that are created, you could use the following table. The order of the elements needs to match the enum:
static Func<IElement>[] Make = {
	()=>{return new BitElement();},
	()=>{return new StringElement();},
	()=>{return new NumberElement();}
};
to create an instance you would use the following code:
ElementType type = ElementType.String;
IElement element = Make[(Int32)type]();
This would avoid reflection.
--
Christof
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform