generics & readonly

A

adrin

I want to make a generic type:

class Prunus<T, S> : BaseClass
where T : TBaseClass
where S : SBaseClass
{
public void DoSomething()
{
// the problem is here
T t = new T(arg1, arg2, ...);
}
}

In the code above, T(arg1, arg2, ...) is a constructor declared in
TBaseClass.
Unfortunately the args (arg1, arg2, ...) must be supplied to the constructor
(they are readonly fields in TBaseClass with only a getter supplied and so I
can't modify them) and using the new() constraint for T does not solve the
problem.
TBaseClass is a class in the BCL (think TreeViewEventArgs & co)... so I
can't change it :(

Any ideea on how to solve this problem?
 
M

Mickey Williams [C# MVP]

Any ideea on how to solve this problem?

At the current time there's no way to have a constraint on a non-default
constructor. You are, as far as I can tell, the 952nd person to request it.
You could build a proxy or a handle for your T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top