I have many classes that are stored in lists. I’m attempting to create a method that accepts a List as a generic type and returns the contents of every element within in the appropriate list as a string.
This is a sample of one of many classes. This would be defined as List<Player>:
public class Player
{
public int PlayerId { get; set; }
public string PlayerName { get; set; }
public string FavoriteThing { get; set; }
}
I’m still struggling to get the syntax correct here:
public static string GetStringOfData<T>(List<T> data)
{
string dataString = string.Empty;
var type = data.GetType();
// Insert magic here to get values for PlayerId, PlayerName, etc...
return dataString;
}
How can we make this happen? I’m using C# 4.0.
Updates
The classes I’m using are generated from an edmx model, connecting to a database. This is for a reporting tool that will parse various tables and report errors to a user via email. The users have specifically requested the ability to see the contents of all fields.
You should be able to do the following: