Generics and Constraints
-
I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument? For instance; public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D
-
I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument? For instance; public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D
Your research is quite (and annoyingly) correct.
I'm largely language agnostic
After a while they all bug me :doh:
-
Your research is quite (and annoyingly) correct.
I'm largely language agnostic
After a while they all bug me :doh:
-
I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument? For instance; public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D
Your post might imply a factory problem, have you considered a factory solution in your design? Also "Object" is un-typed. It seems that mixing it's use with Generics which are a "Typed" solution might be contradictory depending on the unknown requirements of your project.
-
I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument? For instance; public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D
Unfortunately, as you've found out, the new constraint only states that the constrained object must implement the default parameterless constructor. When you think about it, you can see why it behaves the way that it does. Obviously, having a specific constraint on a constructor would mean that you would be constraining it to a particular type and this type may end up being orthoganal to your generic type. Of course, you could simulate this by doing the following:
public class MyClass<T, U> where T : new() where U : MyConstrainedClass { public MyClass(U item) { } }
It's not exactly a neat solution, but it may go someway towards what you are trying to achieve.
Deja View - the feeling that you've seen this post before.
-
Your post might imply a factory problem, have you considered a factory solution in your design? Also "Object" is un-typed. It seems that mixing it's use with Generics which are a "Typed" solution might be contradictory depending on the unknown requirements of your project.
Thanks for your reply, but I guess I should've been more clear with my original message. I don't want to pass a literal Object, I would like to pass an instance of a class that is used for interaction with a database that a coworker has written. I am not too enthusiastic about using these objects, but I am required to. I was at least trying to find a way that would make it a little less painstaking. Thanks for your help though.