instantiating a generic object [modified]
-
Hi guys, i have a method like this: public List listObj() {} and i want to instantiate a object with this type T, i tried this: T obj = new T(); doesnt work, so i tried T obj = default(T); and doesnt work too... how can i instantiete this obj??? Thanks. The code: public class MysqlDireto { private static MysqlDireto _instancia; private static string connectionstring = "Server=;Database=;Uid=;Pwd=;"; public static MysqlDireto Instancia { get { return _instancia ?? (_instancia = new MysqlDireto()); } } public List<T> ObterLista<T>() { MySqlConnection conn = new MySqlConnection(connectionstring); string comando = "SELECT * FROM " + typeof(T).Name; MySqlCommand cmd = new MySqlCommand(comando, conn); conn.Open(); MySqlDataReader leitor = cmd.ExecuteReader(); List<T> lista = new List<T>(); if (leitor.HasRows) { while (leitor.Read()) { T obj = Foo<T>(); obj.GetType().GetProperty("Id").SetValue(obj, leitor["Id"], null); lista.Add(obj); } } conn.Close(); return lista; } public T Foo<T>() where T : class, new() { return new T(); } }
modified on Wednesday, June 8, 2011 2:11 PM
-
Hi guys, i have a method like this: public List listObj() {} and i want to instantiate a object with this type T, i tried this: T obj = new T(); doesnt work, so i tried T obj = default(T); and doesnt work too... how can i instantiete this obj??? Thanks. The code: public class MysqlDireto { private static MysqlDireto _instancia; private static string connectionstring = "Server=;Database=;Uid=;Pwd=;"; public static MysqlDireto Instancia { get { return _instancia ?? (_instancia = new MysqlDireto()); } } public List<T> ObterLista<T>() { MySqlConnection conn = new MySqlConnection(connectionstring); string comando = "SELECT * FROM " + typeof(T).Name; MySqlCommand cmd = new MySqlCommand(comando, conn); conn.Open(); MySqlDataReader leitor = cmd.ExecuteReader(); List<T> lista = new List<T>(); if (leitor.HasRows) { while (leitor.Read()) { T obj = Foo<T>(); obj.GetType().GetProperty("Id").SetValue(obj, leitor["Id"], null); lista.Add(obj); } } conn.Close(); return lista; } public T Foo<T>() where T : class, new() { return new T(); } }
modified on Wednesday, June 8, 2011 2:11 PM
you have to add new() in the where clause of the generic definition.
public T Foo() where T : new(){
return new T();
}Or such
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
you have to add new() in the where clause of the generic definition.
public T Foo() where T : new(){
return new T();
}Or such
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
i got this msg: Error 800 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
I think the error message is clear about what you need to fix: pass your generic class a type that can be instantiated with new T() (i.e. a non-abstract, with a parameterless constructor).
-
i got this msg: Error 800 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
public class Test {
public Test() {} } public class Bar { public static void Test<T>() where T : Test, new() { T t = new T(); } }
...
Bar.Test <Test>();Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
I think the error message is clear about what you need to fix: pass your generic class a type that can be instantiated with new T() (i.e. a non-abstract, with a parameterless constructor).
-
public class Test {
public Test() {} } public class Bar { public static void Test<T>() where T : Test, new() { T t = new T(); } }
...
Bar.Test <Test>();Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
The class that you pass as the generic parameter for T must be like the 'Test' class in the example that Ennis posted below.