[英]Getting the value of a tuple using reflection
所以我試圖使用反射來獲取元組的值,唯一的問題是我得到了一個異常:System.Reflection.TargetInvocationException。 我已經嘗試按照此處建議的一條評論獲取其值: Casting to a Tuple<object,object> , var itemX = t.GetProperty("ItemX").GetValue(data);
如果我使用 lem.FieldType.GetProperty("Item1").Name ,我可以將名稱恢復為 Item1, Item2 等...,我是否正確使用它還是有其他方法?
FieldInfo[] fields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
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());
}
還有我的元組:
protected Tuple<string,int, int, int> testTuple = new Tuple<string, int, int, int>("Test",1,0,1);
讓我們查詢tuple
的Item1..ItemN
屬性; 我們可以借助Linq和正則表達式來完成,例如
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
...
Dictionary<string, object> result = testTuple
.GetType()
.GetProperties()
.Where(prop => prop.CanRead)
.Where(prop => !prop.GetIndexParameters().Any())
.Where(prop => Regex.IsMatch(prop.Name, "^Item[0-9]+$"))
.ToDictionary(prop => prop.Name, prop => prop.GetValue(testTuple));
是時候將它包裝到您的方法中了:
...
foreach (var tuple in tuples) {
var result = tuple
.GetType()
.GetProperties()
.Where(prop => prop.CanRead)
.Where(prop => !prop.GetIndexParameters().Any())
.Where(prop => Regex.IsMatch(prop.Name, "^Item[0-9]+$"))
.Select(prop => new {
name = prop.Name,
value = prop.GetValue(tuple),
});
foreach (var item in result)
Debug.WriteLine($"{item.name} = {item.value}");
}
...
編輯:讓我們從獲取Tuple<,...,>
類型的所有字段開始(請參閱下面的評論):
Object objectToInspect = this;
HashSet<Type> typleTypes = new HashSet<Type>() {
typeof(Tuple<>),
typeof(Tuple<,>),
typeof(Tuple<,,>),
typeof(Tuple<,,,>),
typeof(Tuple<,,,,>),
typeof(Tuple<,,,,,>),
typeof(Tuple<,,,,,,>),
typeof(Tuple<,,,,,,,>),
};
var fieldsWithTuples = objectToInspect
.GetType()
.GetFields(BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.Static |
BindingFlags.FlattenHierarchy)
.Where(field => field.FieldType.IsGenericType)
.Where(field => typleTypes.Contains(field.FieldType.GetGenericTypeDefinition()))
.Select(field => new {
name = field.Name,
value = field.GetValue(field.IsStatic
? null // we should provide null for static
: objectToInspect)
})
.Where(item => item.value != null);
// .Select(item => item.value) // if you want tuple values
// .ToArray(); // materialized as an array
現在我們准備好使用我上面的代碼:
foreach (var tuple in fieldsWithTuples.Select(f => f.value)) {
var result = tuple
.GetType()
.GetProperties()
.Where(prop => prop.CanRead)
.Where(prop => !prop.GetIndexParameters().Any())
.Where(prop => Regex.IsMatch(prop.Name, "^Item[0-9]+$"))
.Select(prop => new {
name = prop.Name,
value = prop.GetValue(tuple),
});
foreach (var item in result)
Debug.WriteLine($"{item.name} = {item.value}");
}
元組的定義如下: Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>
。
遵循元組內的值數量,其邏輯不同,因此要在所有情況下(即使有 8 個項目或更多)通過反射讀取元組,只需使用這段代碼(通用編碼):
public IEnumerable<object> EnumerateValueTuple(object valueTuple)
{
var tuples = new Queue<object>();
tuples.Enqueue(valueTuple);
while (tuples.Count > 0 && tuples.Dequeue() is object tuple)
{
foreach (var field in tuple.GetType().GetFields())
{
if (field.Name == "Rest")
tuples.Enqueue(field.GetValue(tuple));
else
yield return field.GetValue(tuple);
}
}
}
你可以使用:
var item = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
foreach(var value in EnumerateValueTuple(item))
Console.Out.WriteLine(value); // Prints "1 2 3 4 5 6 7 8 9 10 11"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.