First off, your MyClass
has fields, not properties. You'll have to change those to public int i {get; set;}
, for example.
Now, you can use reflection to get the value and type of a property identified by its name.
foreach(MyClass obj in lstSource)
{
foreach(string propertyName in wantedProperties)
{
PropertyInfo property = typeof(MyClass).GetProperty(propertyName);
object value = property.GetValue(obj);
Type propertyType = property.PropertyType;
}
}
If the property is not public, you'll have to use a different overload of GetProperty
and use the BindingFlags.NonPublic | BindingFlags.Instance
binding flags.
Now all you have to do is build the result set.