How to iterate over items in a Tuple, when I don't know at compile-time what are the types the Tuple is composed of? I just need an IEnumerable of objects (for serialization).
private static IEnumerable TupleToEnumerable(object tuple)
{
Type t = tuple.GetType();
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Tuple<,>))
{
var x = tuple as Tuple<object, object>;
yield return x.Item1;
yield return x.Item2;
}
}
You can access properties and their values by reflection with Type.GetProperties
var values = tuple.GetType().GetProperties().Select(p => p.GetValue(tuple));
So your method will be very simple Linq query
private static IEnumerable TupleToEnumerable(object tuple)
{
// You can check if type of tuple is actually Tuple
return tuple.GetType()
.GetProperties()
.Select(property => property.GetValue(tuple));
}