Click here to Skip to main content
15,894,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning all,
I am currently playing around with a splash screen wrapped in a manager class.
This manager class has a factory method and everything works fine.
C#
public static SplashScreenManager Factory(SplashScreenManagerOptions splashScreenManagerOptions)


Now I am thinking that it would be nice to pass in a parameter in order to let the user choose what kind of screen he/she want's to use.
C#
public static SplashScreenManager Factory(SplashScreenBase splashScreenBase, SplashScreenManagerOptions splashScreenManagerOptions)


Problem here is that I have to obtain the Type of the splashScreenBase and then create a new instance of it using reflection, due to the fact that the splashScreenForm will be run in it's own thread. So the user has to pass in an instance only to have another instance created... even if that works fine I do not feel good because I do not know what kind of initializing and stuff will have to happen twice.

So I thought about using constraints. Here's what I've come up with so far:
C#
public static SplashScreenManager Factory<T>(T splashScreenFormType, SplashScreenManagerOptions splashScreenManagerOptions) where T : SplashScreenBase

Ok, this doesn't do what I was thinking about... it only restricts the paramter to be of type SplashScreenBase, not a surprise really...


What I really wanted to do is the following.
The parameter should be of type Type and should be assignable from SplashScreenBase...
So basically it should be a restricted parameter typeof(Type) to only contain certain Types.
Does that makes sense? I hope so ;)


UPDATE:
here is a better illustration of what I want to do:
C#
public static SplashScreenManager Factory<T>(T splashScreenFormType, SplashScreenManagerOptions splashScreenManagerOptions) where T : Type [&& T is a Type that is derived from SplashScreenBase]

This code of course does not compile, but it should show what the goal is :)

Can that be done somehow? In the constraints docs I couldn't find anything that would let me archive my goal... if it is ever doable...

Andy thoughts are kindly appreciated,
cheers
Andy
Posted
Updated 23-May-13 20:23pm
v2

I think you're close.
Something like:
C#
public static SplashScreenManager Factory<T>(SplashScreenManagerOptions splashScreenManagerOptions) where T : SplashScreenBase

This will allow T to be any type that is, or derives from, SplashScreenBase.
You would just need to specify the actual type when Factory was called:
C#
var manager = SplashScreenManager.Factory<SplashScreenBase_Derived_Type>(options...);

You might need add the new condition to the where so you can create the actual class instance.
 
Share this answer
 
Comments
hoernchenmeister 24-May-13 2:14am    
Thanks for your input Matt,
this is basically the same as the third part of code I posted in my question. This should only serve as a demo somehow, because I actually want the parameter be of type "Type".
The user should pass in (typeof(SplashScreenBase)) and the Type should be restricted to Types derived from SplashScreenBase...
SplashScreenBase is actually a Form. In the application I have to launch the form that should serve as SPlashScreen in a different thread using Application.Run(new SplashScreenBase). If the user passes me an instance I can not use it due to cross threading issues, so I would have to read the type of the SplashScreenBase and create a new instance via reflection in order to run it.
The reason for me wanting to use Type as parameter is that in this way only one instance of the SplashScreenBase form has to be created, avoiding whatever has to be done in the initialization of the form...
I updated my question accordingly.

Please excuse if I somehow misunderstood you in any way

I really appreciated your help Matt, thanks a lot.
cheers
Andy
Matt T Heffron 25-May-13 21:41pm    
It appears that you are still misunderstanding what the where T: xxx does.
The xxx specifies the base type restriction, so specifying SplashScreenBase as the xxx will do what you wanted, in that the T will be restricted to any type derived from SplashScreenBase.
If you add the new restriction also, then you can ensure that the Factory method will be able to create the instance of the class in the appropriate thread.
The difference between yours and mine is that you provide the instance of the SplashScreenBase-derived class,
and mine explicitly specifies the type parameter in the call, avoiding the need for passing an actual instance, and allowing for the method to create its own instance.
hoernchenmeister 11-Jun-13 5:25am    
Hi Matt,
first of all, sorry for the late reply.
...and thanks a lot for your explanation, I think I got it now ;)
cheers
Andy
You might try out something like How to do template specialization in C#[^].
Cheers
Andi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900