C# 函数创建属性树?
我正在尝试为对象创建属性列表。我可以为基本对象创建一个列表,但是如果我遇到这样的情况,类型对象a包含类型对象B的属性,而类型对象B包含类型对象a的属性。。。。好然后我得到无限递归。 我所做的是在我的所有对象中添加一个名为“RecursivePropertyList”的静态属性,它是具有递归的每个属性的C# 函数创建属性树?,c#,class,reflection,recursion,properties,C#,Class,Reflection,Recursion,Properties,我正在尝试为对象创建属性列表。我可以为基本对象创建一个列表,但是如果我遇到这样的情况,类型对象a包含类型对象B的属性,而类型对象B包含类型对象a的属性。。。。好然后我得到无限递归。 我所做的是在我的所有对象中添加一个名为“RecursivePropertyList”的静态属性,它是具有递归的每个属性的列表 例如: 我有一个Person类,其属性名为Vendor,类型为Vendor。 Vendor类有一个名为People的属性,其类型为List 在Person类中,我将RecursivePrope
列表
例如:
我有一个Person
类,其属性名为Vendor
,类型为Vendor
。
Vendor
类有一个名为People
的属性,其类型为List
在Person
类中,我将RecursivePropertyList
的值设置为myList.Add(“Vendor.People”)
。
在Vendor
类中,我将RecursivePropertyList
的值设置为myList.Add(“People.Vendor”)
然后在创建属性列表的函数中,我检查当前objectname.propertyName是否位于RecursivePropertyList
中,如果是,我不会将其添加到属性列表中,也不会深入查看以获取其属性
这在上述情况下非常有效
但是,当我遇到以下情况时,它开始失败:
一个应用程序
类,其属性类型为列表
。
一个ApplicationFunction
类,其属性类型为List
。
一个ApplicationFunction\u Application
类,它有两个属性,一个是Application
类型,另一个是ApplicationFunction
类型。
ApplicationFunction\u Application
类的目的是定义Application
和ApplicationFunction
之间的多对多关系
在应用程序的RecursivePropertyList
中,我输入:
myList.Add(“ApplicationFunctions.Application”)代码>
在ApplicationFunction
的RecursivePropertyList
中,我放入:
myList.Add(“Applications.ApplicationFunction”)代码>
在ApplicationFunction\u应用程序的RecursivePropertyList
中,我放入:
myList.Add(“ApplicationFunction.Applications”)代码>
myList.Add(“Application.ApplicationFunctions”)代码>
但是,我的脚本一直在循环,从不停止
以下是该函数使用的代码:
首先,调用的函数开始整个过程:
public static EquatableList<PropertyState> FillPropertyStateList(System.Type myObjectType, ref EquatableList<PropertyState> myPropertyStateList)
{
List<string> myRecursivePropertyList = FillDefaultRecursivePropertyList(myObjectType);
return FillPropertyStateList(myObjectType, ref myPropertyStateList, string.Empty, string.Empty, myRecursivePropertyList);
}
公共静态EquatableList FillPropertyStateList(System.Type MyObject类型,ref EquatableList myPropertyStateList)
{
List myRecursivePropertyList=FillDefaultRecursivePropertyList(MyObject类型);
返回FillPropertyStateList(myObjectType,ref myPropertyStateList,string.Empty,string.Empty,myRecursivePropertyList);
}
然后是一个函数,它完成了大部分工作,并递归地调用自己来生成所有子对象的属性列表
public static EquatableList<PropertyState> FillPropertyStateList(System.Type myObjectType, ref EquatableList<PropertyState> myPropertyStateList, string myParentPrefix, string myObjectName, List<string> myRecursivePropertyList)
{
if (myPropertyStateList == null)
{
myPropertyStateList = new EquatableList<PropertyState>();
}
if (string.IsNullOrEmpty(myParentPrefix))
{
myParentPrefix = string.Empty;
}
else if (!myParentPrefix.EndsWith("."))
{
myParentPrefix = myParentPrefix + ".";
}
if (string.IsNullOrEmpty(myObjectName))
{
myObjectName = string.Empty;
}
else
{
myObjectName = myObjectName + ".";
}
foreach (System.Reflection.PropertyInfo info in myObjectType.GetProperties())
{
if (info.PropertyType.BaseType == typeof(BOBase))
{
if (!myRecursivePropertyList.Exists(delegate(string x) { return x.Equals(myObjectName + info.Name); }))
{
myPropertyStateList.Add(new PropertyState(myParentPrefix + myObjectName + info.Name, true, PropertyType.BOBase));
List<string> myChildRecursivePropertyList = FillDefaultRecursivePropertyList(info.PropertyType);
myChildRecursivePropertyList.AddRange(myRecursivePropertyList.FindAll(delegate(string x) { return Regex.IsMatch(x, info.Name + ".*"); }));
FillPropertyStateList(info.PropertyType, ref myPropertyStateList, myParentPrefix + myObjectName, info.Name, myChildRecursivePropertyList);
}
}
else if (info.PropertyType.IsGenericType
&& (info.PropertyType.BaseType.GetGenericTypeDefinition() == typeof(List<>) || info.PropertyType.BaseType.GetGenericTypeDefinition() == typeof(Library.EquatableList<>)))
{
if (!myRecursivePropertyList.Exists(delegate(string x) { return x.Equals(myObjectName + info.Name); }))
{
myPropertyStateList.Add(new PropertyState(myParentPrefix + myObjectName + info.Name, true, PropertyType.BOBaseCollection));
List<string> myChildRecursivePropertyList = FillDefaultRecursivePropertyList(info.PropertyType.BaseType.GetGenericArguments()[0]);
myChildRecursivePropertyList.AddRange(myRecursivePropertyList.FindAll(delegate(string x) { return Regex.IsMatch(x, info.Name + ".*"); }));
FillPropertyStateList(info.PropertyType.BaseType.GetGenericArguments()[0], ref myPropertyStateList, myParentPrefix + myObjectName, info.Name, myChildRecursivePropertyList);
}
}
else
{
myPropertyStateList.Add(new PropertyState(myParentPrefix + myObjectName + info.Name, true, PropertyType.Standard));
}
}
return myPropertyStateList;
}
公共静态EquatableList FillPropertyStateList(System.Type myObjectType,ref EquatableList myPropertyStateList,string myParentPrefix,string myObjectName,List myRecursivePropertyList)
{
如果(myPropertyStateList==null)
{
myPropertyStateList=新的equalTableList();
}
if(string.IsNullOrEmpty(myParentPrefix))
{
myParentPrefix=string.Empty;
}
如果(!myParentPrefix.EndsWith(“.”),则为else
{
myParentPrefix=myParentPrefix+”;
}
if(string.IsNullOrEmpty(myObjectName))
{
myObjectName=string.Empty;
}
其他的
{
myObjectName=myObjectName+“;
}
foreach(myObjectType.GetProperties()中的System.Reflection.PropertyInfo信息)
{
if(info.PropertyType.BaseType==typeof(BOBase))
{
如果(!myRecursivePropertyList.Exists(委托(字符串x){return x.Equals(myObjectName+info.Name);}))
{
添加(新的PropertyState(myParentPrefix+myObjectName+info.Name,true,PropertyType.BOBase));
List myChildRecursivePropertyList=FillDefaultRecursivePropertyList(info.PropertyType);
myChildRecursivePropertyList.AddRange(myRecursivePropertyList.FindAll(委托(字符串x){return Regex.IsMatch(x,info.Name+“*”;}));
FillPropertyStateList(info.PropertyType,ref myPropertyStateList,myParentPrefix+myObjectName,info.Name,myChildRecursivePropertyList);
}
}
else if(info.PropertyType.IsGenericType
&&(info.PropertyType.BaseType.GetGenericTypeDefinition()==typeof(List)| | info.PropertyType.BaseType.GetGenericTypeDefinition()==typeof(Library.equalTableList)))
{
如果(!myRecursivePropertyList.Exists(委托(字符串x){return x.Equals(myObjectName+info.Name);}))
{
添加(新的PropertyState(myParentPrefix+myObjectName+info.Name,true,PropertyType.BOBaseCollection));
List myChildRecursivePropertyList=FillDefaultRecursivePropertyList(info.PropertyType.BaseType.GetGenericArguments()[0]);
myChildRecursivePropertyList.AddRange(myRecursivePropertyList.FindAll(委托(字符串x){return Regex.IsMatch(x,info.Name+“*”;}));
FillPropertyStateList(info.PropertyType.BaseType.GetGenericArguments()[0],ref myPropertyStateList,myParentPrefix+myObjectName,info.Name,myChildRecursivePropertyList);
private static List<string> FillDefaultRecursivePropertyList(System.Type myObjectType)
{
List<string> myRecursivePropertyList = new List<string>();
if (myObjectType.BaseType == typeof(BOBase))
{
System.Reflection.PropertyInfo pi = myObjectType.GetProperty("RecursivePropertyList", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
if (pi != null)
{
myRecursivePropertyList = (List<string>)pi.GetValue(null, null);
}
}
return myRecursivePropertyList;
}
[AttributeUsage(AttributeTargets.Property)]
public class RecursivePropertyAttribute
: Attribute
{
private static Dictionary<Type, List<PropertyInfo>> _Cache = new Dictionary<Type, List<PropertyInfo>>();
public IEnumerable<PropertyInfo> GetRecursiveProperties(Type t)
{
// Check the cache for the type
if (!_Cache.ContainsKey(t))
{
// Create the entry
_Cache.Add(t, new List<PropertyInfo>());
// Add properties that have the attribute
foreach (PropertyInfo p in t.GetProperties())
{
if (p.IsDefined(typeof(RecursivePropertyAttribute), true))
_Cache[t].Add(p);
}
}
return _Cache[t];
}
}