Skip to main content
major typo -____-
Source Link
James Jones
  • 8.8k
  • 6
  • 36
  • 46

Since nobody bothered to post the 'Reflection' answer (which I personally think is the best answer), here goes:

public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { Type classType = typeof(T); ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() }); T classInstance = (T)classConstructor.Invoke(new object[] { listItem }); tabListItems.Add(classInstance); } ... } 

Edit: This answer is deprecated due to .NET 43.5's Activator.CreateInstance, however it is still useful in older .NET versions.

Since nobody bothered to post the 'Reflection' answer (which I personally think is the best answer), here goes:

public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { Type classType = typeof(T); ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() }); T classInstance = (T)classConstructor.Invoke(new object[] { listItem }); tabListItems.Add(classInstance); } ... } 

Edit: This answer is deprecated due to .NET 4.5's Activator.CreateInstance, however it is still useful in older .NET versions.

Since nobody bothered to post the 'Reflection' answer (which I personally think is the best answer), here goes:

public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { Type classType = typeof(T); ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() }); T classInstance = (T)classConstructor.Invoke(new object[] { listItem }); tabListItems.Add(classInstance); } ... } 

Edit: This answer is deprecated due to .NET 3.5's Activator.CreateInstance, however it is still useful in older .NET versions.

added 130 characters in body
Source Link
James Jones
  • 8.8k
  • 6
  • 36
  • 46

Since nobody bothered to post the 'Reflection' answer (which I personally think is the best answer), here goes:

public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { Type classType = typeof(T); ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() }); T classInstance = (T)classConstructor.Invoke(new object[] { listItem }); tabListItems.Add(classInstance); } ... } 

Edit: This answer is deprecated due to .NET 4.5's Activator.CreateInstance, however it is still useful in older .NET versions.

Since nobody bothered to post the 'Reflection' answer (which I personally think is the best answer), here goes:

public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { Type classType = typeof(T); ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() }); T classInstance = (T)classConstructor.Invoke(new object[] { listItem }); tabListItems.Add(classInstance); } ... } 

Since nobody bothered to post the 'Reflection' answer (which I personally think is the best answer), here goes:

public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { Type classType = typeof(T); ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() }); T classInstance = (T)classConstructor.Invoke(new object[] { listItem }); tabListItems.Add(classInstance); } ... } 

Edit: This answer is deprecated due to .NET 4.5's Activator.CreateInstance, however it is still useful in older .NET versions.

Source Link
James Jones
  • 8.8k
  • 6
  • 36
  • 46

Since nobody bothered to post the 'Reflection' answer (which I personally think is the best answer), here goes:

public static string GetAllItems<T>(...) where T : new() { ... List<T> tabListItems = new List<T>(); foreach (ListItem listItem in listCollection) { Type classType = typeof(T); ConstructorInfo classConstructor = classType.GetConstructor(new Type[] { listItem.GetType() }); T classInstance = (T)classConstructor.Invoke(new object[] { listItem }); tabListItems.Add(classInstance); } ... }