Hi group
I have created a generic control such as this one :
public class SuperControl<E> : Control{}
It work very fine, but i can't have it in design time.
I saw on this group that it is a known problem, and that the designer
don't know how to work with generics. My first question is Have you
found a solution, or a work around, since the last post about this
problem ?
I have start with a new class :
public class SuperControl2 : SuperControl<object>
{
public SuperControl2()
{
}
}
(full implementation).
and i can have it in my design time, it works fine, but it is not very
usefull a runtime.
i create a designer class to help SuperControl2 to generate some good
code (with real class instead of object). My goal is to make all the
design using the SuperControl2, witch exactly look like another
SuperControl<>, but the compilation give the good
SuperControl<SuperClass>. I think about the code generated about this
SuperControl2 contains code like this one :
#if DESIGN
MyControl = new SuperControl2();
#else
MyControl = new SuperControl2<SuperClass>();
#endif
I don't know how to influence the code generation, i have to learn more
about that now. I have currently no idea about all that.
Please tell me if i am crazy or idiot :) or if i have miss something.
Really please
Is it a good way to start ?
Is it a good solution ?
is it a big job ?
have you any idea about this problem/solution ?
please give me some link about that.
thank you!
ROM
Dave Sexton - 13 Aug 2006 21:10 GMT
Hi ROM,
I recommend choosing a better architecture to begin with, specifically one that is polymorphic.
Determine the functionality that you need from the Typed parameter implementation and create either an interface or abstract class
to encapsulate that functionality, such as ISuperControlTask. Declare a protected factory method for its creation within
SuperControl. This architecture is used by the framework in many cases and will probably suit your needs well, but you have many
choices in this matter and you should analyze your situation carefully and choose the best one.
Here's a quick example that makes use of an abstract class, lazy initialization, a factor method and a private, default
implementation.
If you need to extend the SuperControl or a SuperControlTask to add more functionality you can simply create two more classes that
inherit from each.
HTH
public abstract class SuperControlTask
{
public abstract void PerformTask();
}
public class SuperControl : Control
{
protected SuperControlTask Task
{
get
{
EnsureTask();
return task;
}
}
private SuperControlTask task;
public void PerformTask()
{
EnsureTask();
task.PerformTask();
}
private void EnsureTask()
{
if (task == null)
{
// lazy task initialization
task = CreateTask();
if (task == null)
throw new InvalidOperationException("The SuperControl is not associated with a SuperControlTask.");
}
}
protected virtual SuperControlTask CreateTask()
{
return new DefaultSuperControlTask();
}
private class DefaultSuperControlTask : SuperControlTask
{
public override void PerformTask()
{
// TODO: default task
}
}
}

Signature
Dave Sexton
> Hi group
>
[quoted text clipped - 47 lines]
>
> ROM