我有一个用例,我需要检查一个值是否为C#7 ValueTuple,如果是,则循环遍历每个项目。我尝试使用obj is ValueTuple
和obj is (object, object)
进行检查,但这两个都返回false。我发现我可以使用obj.GetType().Name
并检查它是否以"ValueTuple"
开头,但这对我来说似乎很蹩脚。任何替代方案都会受到欢迎。
我也有获得每件物品的问题。我尝试使用此处找到的解决方案获取Item1
:How do I check if a property exists on a dynamic anonymous type in c#?但((dynamic)obj).GetType().GetProperty("Item1")
返回null。我希望我能够while
来获取每件物品。但这不起作用。我如何获得每件商品?
更新 - 更多代码
if (item is ValueTuple) //this does not work, but I can do a GetType and check the name
{
object tupleValue;
int nth = 1;
while ((tupleValue = ((dynamic)item).GetType().GetProperty($"Item{nth}")) != null && //this does not work
nth <= 8)
{
nth++;
//Do stuff
}
}
答案 0 :(得分:9)
结构不会在C#中继承,因此ValueTuple<T1>
,ValueTuple<T1,T2>
,ValueTuple<T1,T2,T3>
等是不以ValueTuple
为基础的不同类型。因此,obj is ValueTuple
检查失败。
如果您正在寻找具有任意类型参数的ValueTuple
,您可以按如下方式检查该类是否为ValueTuple<,...,>
:
private static readonly Set<Type> ValTupleTypes = new HashSet<Type>(
new Type[] { typeof(ValueTuple<>), typeof(ValueTuple<,>),
typeof(ValueTuple<,,>), typeof(ValueTuple<,,,>),
typeof(ValueTuple<,,,,>), typeof(ValueTuple<,,,,,>),
typeof(ValueTuple<,,,,,,>), typeof(ValueTuple<,,,,,,,>)
}
);
static bool IsValueTuple2(object obj) {
var type = obj.GetType();
return type.IsGenericType
&& ValTupleTypes.Contains(type.GetGenericTypeDefinition());
}
要根据类型获取子项,您可以使用一种不是特别快的方法,但应该这样做:
static readonly IDictionary<Type,Func<object,object[]>> GetItems = new Dictonary<Type,Func<object,object[]>> {
[typeof(ValueTuple<>)] = o => new object[] {((dynamic)o).Item1}
, [typeof(ValueTuple<,>)] = o => new object[] {((dynamic)o).Item1, ((dynamic)o).Item2}
, [typeof(ValueTuple<,,>)] = o => new object[] {((dynamic)o).Item1, ((dynamic)o).Item2, ((dynamic)o).Item3}
, ...
};
这可以让你这样做:
object[] items = null;
var type = obj.GetType();
if (type.IsGeneric && GetItems.TryGetValue(type.GetGenericTypeDefinition(), out var itemGetter)) {
items = itemGetter(obj);
}
答案 1 :(得分:6)
关于问题的部分&#34;我如何获得每个项目?&#34; ...
ValueTuple和Tuple都实现// SUT (as a local function)
IEnumerable<object> GetValuesFromTuple(System.Runtime.CompilerServices.ITuple tuple)
{
for (var i = 0; i < tuple.Length; i++)
yield return tuple[i];
}
// arrange
var valueTuple = (StringProp: "abc", IntProp: 123, BoolProp: false, GuidProp: Guid.Empty);
// act
var values = GetValuesFromTuple(valueTuple);
// assert (to console)
Console.WriteLine($"Values = '{values.Count()}'");
foreach (var value in values)
{
Console.WriteLine($"Value = '{value}'");
}
,它具有length属性和indexer属性。因此,以下控制台应用程序代码会将值列入控制台:
Values = '4'
Value = 'abc'
Value = '123'
Value = 'False'
Value = '00000000-0000-0000-0000-000000000000'
控制台输出:
public interface IModelConverter<in T> : IConverter where T:IDataCollection
{
ResponseData Activate(T documents, ServiceContext services, bool overrideIfNeeded = false);
bool ContainsFile(T document, ServiceContext services);
}
答案 2 :(得分:4)
这是我解决问题的方法。 PCL兼容的扩展类。特别感谢@dasblinkenlight和@Evk帮助我!
public static class TupleExtensions
{
private static readonly HashSet<Type> ValueTupleTypes = new HashSet<Type>(new Type[]
{
typeof(ValueTuple<>),
typeof(ValueTuple<,>),
typeof(ValueTuple<,,>),
typeof(ValueTuple<,,,>),
typeof(ValueTuple<,,,,>),
typeof(ValueTuple<,,,,,>),
typeof(ValueTuple<,,,,,,>),
typeof(ValueTuple<,,,,,,,>)
});
public static bool IsValueTuple(this object obj) => IsValueTupleType(obj.GetType());
public static bool IsValueTupleType(this Type type)
{
return type.GetTypeInfo().IsGenericType && ValueTupleTypes.Contains(type.GetGenericTypeDefinition());
}
public static List<object> GetValueTupleItemObjects(this object tuple) => GetValueTupleItemFields(tuple.GetType()).Select(f => f.GetValue(tuple)).ToList();
public static List<Type> GetValueTupleItemTypes(this Type tupleType) => GetValueTupleItemFields(tupleType).Select(f => f.FieldType).ToList();
public static List<FieldInfo> GetValueTupleItemFields(this Type tupleType)
{
var items = new List<FieldInfo>();
FieldInfo field;
int nth = 1;
while ((field = tupleType.GetRuntimeField($"Item{nth}")) != null)
{
nth++;
items.Add(field);
}
return items;
}
}