gpt4 book ai didi

c# - 如何检查我的未初始化变量是否为 List 类型?

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:54 25 4
gpt4 key购买 nike

我有以下类定义:

[ActionsClass[typeof(MyActions)]
public class NkProject
{
int ProjectId;
IList<NkItem> Items;
NkItem SingleItem;
}

这个类主要只包含字段,类属性说明哪个类负责“处理”这个类作为更大框架的一部分。

我使用反射解析此文件以生成一些元数据,我想知道“Items”是否属于列表类型(或充当列表的任何类型的集合),但到目前为止我已经尝试过的所有内容失败。我正在遍历一组 FieldInfo[] 对象。

  1. 检查 IEnumerable:


    if(field.FieldType.GetInterface("IEnumerable") != null)

    This also detects strings as IEnumerables, which is incorrect. I'd prefer if strings fail to pass this check and get treated as primitives in a sense (the else of the above if).

  2. Changed above to: if(field is ICollection) and that also fails.

  3. Changed above to: if(field is IList && field.GetType().IsGenericType) and it still fails to be detected correctly.

Is there a way to look only at the type of the variable references to ascertain whether Items is of type List and a parallel check for string Items fails? I'm guessing #2 and #3 above fail because Items is not an object and thus there isn't any inheritance information attached to it. I could be wrong, but I think that's what is happening. A field.GetType().Name returns the type as IList`1. Not sure how to get around this.

EDIT/UPDATE: If the above check fails, and a string goes through, the output meta is incorrectly formatted. Since, if it's a collection/list of items, I need to fetch further metadata for them (via the appropriately set attributes). This step is only executed if the variable is of type List/Collection but not if it's a single reference or a primitive.

Reflection code:


foreach (var field in fieldInfo)
{
Property property;

//If it's a collection field, get the appropriate collection name as set in its attribute
if (field.FieldType.GetInterface("IEnumerable") != null) //currently lets strings pass through too.
{
//Get the type of the "type argument" of the IEnumerable
Type[] typeArgs = field.FieldType.GetGenericArguments();
//Get the collection name for that type to use in the XML
MyAttribute att = (MyAttribute )Attribute.GetCustomAttribute(typeArgs[0], typeof(MyAttribute ));

if (att == null)
throw new ArgumentException("Using collection: {0} in Your class: {1}. Collection must have MyAttribute[Collection=\"\"] attribute defined on its class declaration.");

else
{
//do something with meta data
}
}

//If non-collection, possibly primitive field do something else
else
{
//do other stuff here.
}
}

最佳答案

您可以使用泛型类型信息来检查它是否是一个列表

if(field.FieldType.IsGenericType 
&& field.FieldType.GetGenericTypeDefinition() == typeof(List<>))

目前我的代码库中有以下针对此类情况的函数。

public static bool Closes(this Type type, Type openType)
{
if (type == null)
return false;

if (type.IsGenericType && type.GetGenericTypeDefinition() == openType) return true;

foreach (var @interface in type.GetInterfaces())
{
if (@interface.Closes(openType)) return true;
}

return type.BaseType.Closes(openType);
}

这样你就可以调用

field.FieldType.Closes(typeof(List<>)

关于c# - 如何检查我的未初始化变量是否为 List 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com