Where is the constructor suggestion in C #?

So here is what I am trying to do.

I create a generic class that allocates the type specified by the generic parameter in one of two ways, determined by using the overloaded constructor.

Here is an example:

class MyClass<T> where T : class { public delegate T Allocator(); public MyClass() { obj = new T(); } public MyClass( Allocator alloc ) { obj = alloc(); } T obj; } 

This class requires that type T be reftype in all cases. For the default constructor, we want to instantiate T through its default constructor. I would like to put where T : new() in my default constructor, for example:

 public MyClass() where T : new() { obj = new T(); } 

However, this is not valid C #. Basically, I only want to add a restriction on the type T to have a default constructor only when the default constructor MyClass () is used.

In the second constructor of MyClass, we allow the user to determine how to distribute their own distribution method for T, so it is obvious that MyClass makes sense not to use T in all cases by default.

I have a feeling that for this I need to use reflection in the default constructor, but I hope not.

I know this can be done because the Lazy<T> class in .NET 4.0 does not require T to be constructive by default at the class level, but it does have constructors similar to those shown in my example. I would like to know how Lazy<T> does this at least.

+8
generics c #
source share
4 answers

Only restrictions can be included in an ad in which you enter a type parameter.

However, you can introduce a generic method for a non-generic type:

 public class MyClass { public static MyClass<T> Create<T>() where T : class, new() { return new MyClass<T>(() => new T()); } } public class MyClass<T> where T : class { T obj; public MyClass(Allocator allocator) { obj = allocator(); } } 

(I would just use Func<T> instead of declaring a separate btw delegate type.)

Then you can use:

 MyClass<Foo> foo = MyClass.Create<Foo>(); // Enforces the constraint 
+8
source share

Basically, I only want to add a restriction on the type T to have a default constructor only when the default constructor MyClass () is used.

It is not possible to force this with a restriction on T You either mean that where T : new() is specified in the definition of MyClass<T> , or you do not comply with this restriction at all.

I have a feeling that for this I need to use reflection in the default constructor, but I hope not.

You cannot force the restriction, but you can say

 obj = Activator.CreateInstance<T>(); 

which will call the default constructor for T

I know this can be done because the Lazy class in .NET 4.0 does not require T to be constructive by default at the class level, but it does have constructors similar to those shown in my example. I would like to know how Lazy does this at least.

Lazy<T> is required to specify a delegate that returns T instances. In principle, this requires specifying Func<T> .

+7
source share

You can try using Activator

 class MyClass<T> where T : class { public MyClass() { obj = Activator.CreateInstance<T>(); } } 
+3
source share

Well, you need to either have a limitation or not. Turns out Lazy uses Activator to instantiate. Here's how Lazy<T> remote code works in case of copyright infringement.

-2
source share

All Articles