gpt4 book ai didi

c# - 如何查找(强类型)集合中包含的类型?

转载 作者:行者123 更新时间:2023-11-30 22:20:52 24 4
gpt4 key购买 nike

<分区>

我有一个看起来像这样的类:

public class ObjectA
{
public ObjectB OneObject { get; set; }

public List<ObjectC> ManyObject { get; set; }
}

然后是一个读取类包含的内容并返回属性类型的函数:

source = typeof(*some ObjectA*)

var classprops = source.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.PropertyType.IsClass && !x.PropertyType.IsValueType && x.PropertyType.Name != "String");

foreach (var prop in classprops)
{
var classnamespace = CommonTool.GetNamespaceFromProp(prop);

if ((prop.PropertyType).Namespace == "System.Collections.Generic")
{
string newprop = prop.ToString();
int start = newprop.IndexOf("[")+1;
int end = newprop.IndexOf("]");
newprop = newprop.Substring(start, end-start);
newprop = string.Format("{0}, Env.Project.Entites", newprop);
classnamespace = newprop;
}
//some code to read attributes on the properties...
}

我的问题是 if ((prop.PropertyType).Namespace == "System.Collections.Generic") 中的内容.它闻起来

有什么更好的方法吗?

编辑:

应用程序中的一个类使用了List<int> .
这导致了崩溃。
它不仅闻起来很臭。

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