C# array of properties
arrays, c#, getter, properties
Solution
You could use reflection to access the properties within your type:
class MyType
{
public int prop1 { get; }
public string prop2 { get; }
public int[] prop3 { get; }
public int prop4 { get; }
public string prop5 { get; }
public string prop6 { get; }
public List<string> GetAllPropertyValues()
{
List<string> values = new List<string>();
foreach (var pi in typeof(MyType).GetProperties())
{
values.Add(pi.GetValue(this, null).ToString());
}
return values;
}
}
Note that reflection is slow and you shouldn’t use this if there is a better way. For example when you know that there are only 6 properties, just go through them individually.
Problem
I have several get properties that I would like to be able to loop through like an array of functions. I would like to be able to do something like this ``` public int prop1 { get; } public string prop2 { get; } public int[] prop3 { get; } public int prop4 { get; } public string prop5 { get; } public string prop6 { get; } Func<var> myProperties = { prop1, prop2, prop3, prop4, prop5, prop6 }; ArrayList myList = new ArrayList(); foreach( var p in myProperties) { myList.Add(p); } ``` This code is very broken, but I think it conveys the idea of what I would like to be able to do. Anyone know how I can achieve this?