vendredi 27 décembre 2019

c# Get tuple's value using reflection

so I'm trying to get a tuple's value using reflection, the only problem is that I get an exception: System.Reflection.TargetInvocationException. I've tried getting its value as one comment suggested here: Casting to a Tuple<object,object>, var itemX = t.GetProperty("ItemX").GetValue(data); If I uses lem.FieldType.GetProperty("Item1").Name , I can get the name back as Item1, Item2 , etc.. , am I using it correctly or is there any other way?

FieldInfo[] tuples = fields.Where(field=>typeof(IStructuralEquatable).IsAssignableFrom(field.FieldType) && typeof(IStructuralComparable).IsAssignableFrom(field.FieldType)).ToArray();
                Debug.WriteLine(tuples.Length.ToString()" ->");
                foreach (var elem in tuples)
                {
                    Debug.WriteLine(elem.FieldType.GetProperty("Item1").GetValue(this,null).ToString());

                    PropertyInfo[] info = elem.FieldType.GetProperties();
                    Debug.WriteLine(info[0].GetValue(this,null).ToString());
                    for(int i=0;i<info.Length;i++)
                    {
                        Debug.WriteLine(info[i].GetValue(this,null).ToString());
                    }





Aucun commentaire:

Enregistrer un commentaire