Click here to Skip to main content
15,921,694 members
Home / Discussions / C#
   

C#

 
GeneralCreate Undo functionnality. Pin
HAHAHA_NEXT12-Jul-04 19:56
HAHAHA_NEXT12-Jul-04 19:56 
GeneralRe: Create Undo functionnality. Pin
Roman Rodov13-Jul-04 1:25
Roman Rodov13-Jul-04 1:25 
GeneralWindow Message Pin
sachinkalse12-Jul-04 19:15
sachinkalse12-Jul-04 19:15 
GeneralRe: Window Message Pin
Heath Stewart13-Jul-04 5:22
protectorHeath Stewart13-Jul-04 5:22 
GeneralRe: Window Message Pin
sachinkalse13-Jul-04 16:16
sachinkalse13-Jul-04 16:16 
GeneralRe: Window Message Pin
Heath Stewart14-Jul-04 3:01
protectorHeath Stewart14-Jul-04 3:01 
QuestionRegistering classes with a class factory? Pin
Scythen12-Jul-04 18:01
Scythen12-Jul-04 18:01 
AnswerRe: Registering classes with a class factory? Pin
Heath Stewart13-Jul-04 5:46
protectorHeath Stewart13-Jul-04 5:46 
Typically, this is done using a .config file, where types (as strings, like "System.String, mscorlib") are defined. You could easily create an IConfigurationSectionHandler (or re-use an existing one like the NameValueSectionHandler) that reads these in the first time your configuration section is accessed (it's cached after that, so changes must be to the settings class or the app must be restarted). You could, for example, read these into a class with a static property that is an IList or IDictionary (typed class, if you want) that may be read-only or read-write (if you want to change it at runtime).

When you want to create an instance of a class, you pass some token (perhaps the string name or some friendly name) and the type is created.

This is actually how much of the .NET Framework BCL works, especially in .NET 2.0 (which uses the provider pattern heavily). In our own flagship application I designed, callers pass the type of interface for which a proxy should be instantiated. The configuration class finds the type string for that interface and instantiates the proxy. All calls to the proxy are late-binding calls to the interface.

You should search CodeProject for "configuration settings" or something along those lines for examples.

In this scenario, your settings class (the instance the IConfigurationSectionHandler creates) is the factory. Registration is done by adding types to the .config file, or adding them at runtime (perhaps you want to support some built-in types that should always be present).

If you want to use this provider pattern with the new operator, you should take a look at the ContextBoundObject class documentation in the .NET Framework SDK. By attributing a derivative class with the ProxyAttribute and implementing your own RealProxy, you can intercept IConstructionCallMessage and create an appropriate type. Perhaps the constructor would take a Type or a String, and your RealProxy derivative uses that parameter to actually create that type and return it. Of course, that Type should abide by some contract - perhaps even extending the class that the caller believes they are instantiating.

You can find more information about this at MSDN[^] and here on CodeProject. Search for AOP - or aspect-oriented programming. This isn't AOP specifically, mind you, but uses concepts that are very similar (proxying calls to another context so that transform sinks can be inserted in a chain easily, which is essentially what your class factory is in this scenario).

 

Microsoft MVP, Visual C#
My Articles
GeneralSecurity Access Error Pin
dbetting12-Jul-04 16:48
dbetting12-Jul-04 16:48 
GeneralRe: Security Access Error Pin
Nick Parker12-Jul-04 17:22
protectorNick Parker12-Jul-04 17:22 
Generalcode for adding Pin
stevemasters2212-Jul-04 13:58
stevemasters2212-Jul-04 13:58 
GeneralRe: code for adding Pin
KevinMac12-Jul-04 17:01
KevinMac12-Jul-04 17:01 
GeneralRe: code for adding Pin
stevemasters2212-Jul-04 17:27
stevemasters2212-Jul-04 17:27 
GeneralRe: code for adding Pin
Nick Parker12-Jul-04 17:50
protectorNick Parker12-Jul-04 17:50 
GeneralRe: code for adding Pin
stevemasters2212-Jul-04 20:54
stevemasters2212-Jul-04 20:54 
GeneralRe: code for adding Pin
Nick Parker13-Jul-04 7:10
protectorNick Parker13-Jul-04 7:10 
GeneralEventHandler problem Pin
Jae Sammy12-Jul-04 13:33
Jae Sammy12-Jul-04 13:33 
GeneralRe: EventHandler problem Pin
Nick Parker12-Jul-04 14:21
protectorNick Parker12-Jul-04 14:21 
GeneralRe: EventHandler problem Pin
Jae Sammy12-Jul-04 18:19
Jae Sammy12-Jul-04 18:19 
GeneralRe: EventHandler problem Pin
Jae Sammy13-Jul-04 5:39
Jae Sammy13-Jul-04 5:39 
GeneralRe: EventHandler problem Pin
Nick Parker13-Jul-04 7:16
protectorNick Parker13-Jul-04 7:16 
GeneralRe: EventHandler problem Pin
Jae Sammy13-Jul-04 7:40
Jae Sammy13-Jul-04 7:40 
GeneralRe: EventHandler problem Pin
Nick Parker13-Jul-04 8:29
protectorNick Parker13-Jul-04 8:29 
GeneralRe: EventHandler problem Pin
Jay Shankar12-Jul-04 20:05
Jay Shankar12-Jul-04 20:05 
GeneralProblem with System.Management Pin
Jacob Bang12-Jul-04 12:21
Jacob Bang12-Jul-04 12:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.