A
Andrus
I know entity class name as string ( e.q. "Customer" )
I need to create empty List<Customer> object for this entity by class name
passed as string in runtime.
I tried the following code but got compile error shown in comment.
How to create empty list based on type ?
I hope some reflection method can be used for this ?
Sidenote:
I need to use type t for list creation since my data binding looks for
property names which are different in every entity.
This list is used as DataGridViewComboBoxCell DataSource and filled
dynamically in DataGridView OnDataError event.
I know it is possible to add a method returning empty list for every
entity class by I'm looking for a solution which does to require new method
in every entity class.
Andrus.
Codo to show the problem:
using System.Collections.Generic;
using System;
class TestApplication {
static void Main() {
object CustomerColumnDynamicDataSource =
CreateEmptyListByTypeName("Customer");
object ProductColumnDynamicDataSource =
CreateEmptyListByTypeName("Product");
}
static IList<object> CreateEmptyListByTypeName(string typeName) {
Type t = Type.GetType(typeName);
//Next line causes error:
// The type or namespace name 't' could not be found (are you missing a
//using directive or an assembly reference?)
return (IList<object>)new List<t>();
}
}
class Customer : ActiveRecordBase<Customer> { }
class Product : ActiveRecordBase<Product> { }
public class ActiveRecordBase<T> {}
I need to create empty List<Customer> object for this entity by class name
passed as string in runtime.
I tried the following code but got compile error shown in comment.
How to create empty list based on type ?
I hope some reflection method can be used for this ?
Sidenote:
I need to use type t for list creation since my data binding looks for
property names which are different in every entity.
This list is used as DataGridViewComboBoxCell DataSource and filled
dynamically in DataGridView OnDataError event.
I know it is possible to add a method returning empty list for every
entity class by I'm looking for a solution which does to require new method
in every entity class.
Andrus.
Codo to show the problem:
using System.Collections.Generic;
using System;
class TestApplication {
static void Main() {
object CustomerColumnDynamicDataSource =
CreateEmptyListByTypeName("Customer");
object ProductColumnDynamicDataSource =
CreateEmptyListByTypeName("Product");
}
static IList<object> CreateEmptyListByTypeName(string typeName) {
Type t = Type.GetType(typeName);
//Next line causes error:
// The type or namespace name 't' could not be found (are you missing a
//using directive or an assembly reference?)
return (IList<object>)new List<t>();
}
}
class Customer : ActiveRecordBase<Customer> { }
class Product : ActiveRecordBase<Product> { }
public class ActiveRecordBase<T> {}