A
Andrus
I need to create Factory() method which creates entity objects from
string type names ( e.q. "Customer", "Product" ) for 3.5 WinForms Linq
application.
I tried code below but got error shown in comment.
How to fix this code?
How to create constructor from string type name and call it
using reflection or any other solution ?
Andrus.
class TestApplication {
static void Main() {
object Customer = Factory("Customer");
object Product = Factory("Product");
}
static object Factory(string typeName) {
Type t = Type.GetType(typeName);
// error: type expected.
return new <t>();
}
}
class Customer { public string Name { get; set; } }
class Product { public string Code { get; set; } }
string type names ( e.q. "Customer", "Product" ) for 3.5 WinForms Linq
application.
I tried code below but got error shown in comment.
How to fix this code?
How to create constructor from string type name and call it
using reflection or any other solution ?
Andrus.
class TestApplication {
static void Main() {
object Customer = Factory("Customer");
object Product = Factory("Product");
}
static object Factory(string typeName) {
Type t = Type.GetType(typeName);
// error: type expected.
return new <t>();
}
}
class Customer { public string Name { get; set; } }
class Product { public string Code { get; set; } }