G
Guest
Hi,
I'm porting a fairly hefty n-tier implementation from .Net 1.1 -> .Net 2.0
(and fixing a few things in the process). One of the improvements I am
making is moving the existing non-generic collection code to generics using
the following template code. Basically a collection is built from a
List<type> as required (see code). There is a method called PopulateAll() in
the base collection which fill the collection with objects based on the type
T.
The question is: How do you create an instance of type T within the generic
base with a constructor passing the DataRow to the type's constructor. I am
willing to use any workaround available. The unknown bit is marked #######.
I have tried "new T(row)" but that obviously won't work as a generic is not
a straightforward instantiatable type and you can't do a "new" on it (as the
compiler kindly reminded me).
Simplified code (nasty bracket style) for demonstration...
// base object implementation
public abstract class BaseObject {
internal BaseObject(DataRow row) {
// create the object based on the datarow
}
}
// base collection implementation
public abstract class BaseObjectCollection<T> : List<T> {
public void PopulateAll() {
DataTable dt = new DataTable();
foreach (DataRow dr in dt.Rows) {
// vv What goes in here to instantiate a type of T?
Add(new #######(dr));
}
}
}
// Business object
public class Noggin : BaseObject {
}
// Business object collection
public class NogginCollection : BaseObjectCollection<Noggin> {
}
I'll buy whoever solves this a beer if they can meet me in
Farringdon/Holborn in London, UK!
Cheers,
- Chris.
I'm porting a fairly hefty n-tier implementation from .Net 1.1 -> .Net 2.0
(and fixing a few things in the process). One of the improvements I am
making is moving the existing non-generic collection code to generics using
the following template code. Basically a collection is built from a
List<type> as required (see code). There is a method called PopulateAll() in
the base collection which fill the collection with objects based on the type
T.
The question is: How do you create an instance of type T within the generic
base with a constructor passing the DataRow to the type's constructor. I am
willing to use any workaround available. The unknown bit is marked #######.
I have tried "new T(row)" but that obviously won't work as a generic is not
a straightforward instantiatable type and you can't do a "new" on it (as the
compiler kindly reminded me).
Simplified code (nasty bracket style) for demonstration...
// base object implementation
public abstract class BaseObject {
internal BaseObject(DataRow row) {
// create the object based on the datarow
}
}
// base collection implementation
public abstract class BaseObjectCollection<T> : List<T> {
public void PopulateAll() {
DataTable dt = new DataTable();
foreach (DataRow dr in dt.Rows) {
// vv What goes in here to instantiate a type of T?
Add(new #######(dr));
}
}
}
// Business object
public class Noggin : BaseObject {
}
// Business object collection
public class NogginCollection : BaseObjectCollection<Noggin> {
}
I'll buy whoever solves this a beer if they can meet me in
Farringdon/Holborn in London, UK!
Cheers,
- Chris.