C# 如何获取对象的所有基元类型
我想得到一个对象的所有属性,这些属性都是基元类型的,如果这个对象与另一个类有关系,我想得到另一个类的基元属性 问题是如果实体A有实体B,而B有实体A,我该怎么办 在简单的情况下,通过使用反射,我可以获得一级基本属性,但是,我不能进入实体B并再次获得A的基本属性,将创建一个循环,您提供了什么C# 如何获取对象的所有基元类型,c#,C#,我想得到一个对象的所有属性,这些属性都是基元类型的,如果这个对象与另一个类有关系,我想得到另一个类的基元属性 问题是如果实体A有实体B,而B有实体A,我该怎么办 在简单的情况下,通过使用反射,我可以获得一级基本属性,但是,我不能进入实体B并再次获得A的基本属性,将创建一个循环,您提供了什么 public class A { public string Name{get;set;} public B B{get;set;} } public class B { public strin
public class A
{
public string Name{get;set;}
public B B{get;set;}
}
public class B
{
public string Category{get;set;}
public A A{get;set;}
}
您可以跟踪访问的类型以避免递归:
public class A
{
public string Name { get; set; }
public B B { get; set; }
}
public class B
{
public string Category { get; set; }
public A A { get; set; }
}
class Program
{
static void Main()
{
var result = Visit(typeof(A));
foreach (var item in result)
{
Console.WriteLine(item.Name);
}
}
public static IEnumerable<PropertyInfo> Visit(Type t)
{
var visitedTypes = new HashSet<Type>();
var result = new List<PropertyInfo>();
InternalVisit(t, visitedTypes, result);
return result;
}
private void InternalVisit(Type t, HashSet<Type> visitedTypes, IList<PropertyInfo> result)
{
if (visitedTypes.Contains(t))
{
return;
}
if (!IsPrimitive(t))
{
visitedTypes.Add(t);
foreach (var property in t.GetProperties())
{
if (IsPrimitive(property.PropertyType))
{
result.Add(property);
}
InternalVisit(property.PropertyType, visitedTypes, result);
}
}
}
private static bool IsPrimitive(Type t)
{
// TODO: put any type here that you consider as primitive as I didn't
// quite understand what your definition of primitive type is
return new[] {
typeof(string),
typeof(char),
typeof(byte),
typeof(sbyte),
typeof(ushort),
typeof(short),
typeof(uint),
typeof(int),
typeof(ulong),
typeof(long),
typeof(float),
typeof(double),
typeof(decimal),
typeof(DateTime),
}.Contains(t);
}
}
公共A类
{
公共字符串名称{get;set;}
公共B{get;set;}
}
公共B级
{
公共字符串类别{get;set;}
公共A{get;set;}
}
班级计划
{
静态void Main()
{
var结果=就诊(类型(A));
foreach(结果中的var项目)
{
Console.WriteLine(项目名称);
}
}
公共静态IEnumerable访问(t型)
{
var visitedTypes=newhashset();
var result=新列表();
内部访问(t、访问类型、结果);
返回结果;
}
私有void InternalVisit(类型t、哈希集visitedTypes、IList结果)
{
if(visitedTypes.Contains(t))
{
返回;
}
if(!IsPrimitive(t))
{
访问类型。添加(t);
foreach(t.GetProperties()中的var属性)
{
if(IsPrimitive(property.PropertyType))
{
结果.添加(属性);
}
内部访问(property.PropertyType、访问类型、结果);
}
}
}
专用静态bool IsPrimitive(t型)
{
/ttodo:把任何你认为是原始的类型放在我没有的地方
//完全理解基本类型的定义
返回新的[]{
类型(字符串),
类型(字符),
类型(字节),
类型(sbyte),
类型(ushort),
类型(短),
类型(uint),
类型(int),
类型(ulong),
类型(长),
类型(浮动),
类型(双),
类型(十进制),
类型(日期时间),
}.包含(t);
}
}
我对等待构建的工作感到厌烦,请享受您的家庭作业带来的乐趣;)
名称空间草稿行
{
公共静态类类型提取器
{
公共静态IEnumerable ExtractTypes(此类型所有者,HashSet=null)
{
如果(访问==null)
visited=新HashSet();
如果(已访问。包含(所有者))
返回新类型[0];
已访问。添加(所有者);
开关(类型.GetTypeCode(所有者))
{
案例类型代码。对象:
打破
案例类型代码。空:
返回新类型[0];
违约:
返回新的[]{owner};
}
返回
owner.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance|
BindingFlags.Static|
BindingFlags.FlattleHierarchy)
.SelectMany(x=>x.ExtractTypes(已访问)).Union(新[]{owner}).Distinct();
}
公共静态IEnumerable ExtractTypes(此MemberInfo成员,已访问哈希集)
{
开关(成员.成员类型)
{
案例成员类型。属性:
return((PropertyInfo)成员).PropertyType.ExtractTypes(已访问);
打破
案例成员类型。字段:
return((FieldInfo)member.FieldType.ExtractTypes(已访问);
违约:
返回新类型[0];
}
}
}
公共A类
{
公共字符串名称{get;set;}
公共B{get;set;}
}
公共B级
{
公共字符串类别{get;set;}
公共A{get;set;}
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
var q=typeof(A).ExtractTypes();
foreach(q中的var类型)
{
Console.Out.WriteLine(type.Name);
}
}
}
}
我将作出如下声明:
void Traverse(Type type, ISet<Type> marks, ICollection<PropertyInfo> result)
{
if (marks.Contains(type)) return; else marks.Add(type);
foreach (var propertyInfo in type.GetProperties())
if (propertyInfo.PropertyType.IsPrimitive) result.Add(propertyInfo);
else Traverse(propertyInfo.PropertyType, marks, result);
}
void遍历(类型、ISet标记、ICollection结果)
{
if(marks.Contains(type))返回;else标记.Add(type);
foreach(type.GetProperties()中的var propertyInfo)
if(propertyInfo.PropertyType.IsPrimitive)result.Add(propertyInfo);
else遍历(propertyInfo.PropertyType、标记、结果);
}
及
var props=new List();
遍历(yourRootType,newhashset(),props);
您需要跟踪已检查的类型
public static List<PropertyInfo> ProcessType(Type type)
{
return ProcessType(type, new List<Type>());
}
public static List<PropertyInfo> ProcessType(Type type, List<Type> processedTypes)
{
// Keep track of results
var result = new List<PropertyInfo>();
// Iterate properties of the type
foreach (var property in type.GetProperties())
{
var propertyType = property.PropertyType;
// If the property has a primitive type
if (propertyType.IsPrimitive)
{
// add it to the results
result.Add(property);
}
// If the property has a non-primitive type
// and it has not been processed yet
else if (!processedTypes.Contains(propertyType))
{
// Mark the property's type as already processed
processedTypes.Add(propertyType);
// Recursively processproperties of the property's type
result.AddRange(ProcessType(propertyType, processedTypes));
}
}
return result;
}
公共静态列表ProcessType(类型)
{
返回ProcessType(type,newlist());
}
公共静态列表ProcessType(类型类型,列表处理类型)
{
//跟踪结果
var result=新列表();
//迭代类型的属性
foreach(类型.GetProperties()中的var属性)
{
var propertyType=property.propertyType;
//如果属性具有基元类型
if(propertyType.IsPrimitive)
{
//将其添加到结果中
结果.添加(属性);
}
//如果属性具有非基元类型
//而且它还没有被处理
如果(!processedTypes.Contains(propertyType))包含,则为else
{
//将属性的类型标记为已处理
processedTypes.Add(propertyType);
//递归处理属性类型的属性
AddRange(ProcessType(propertyType,processedTypes));
}
}
返回结果;
}
请定义原语?是System.Stringvar props = new List<PropertyInfo>();
Traverse(yourRootType, new HashSet<Type>(), props);
public static List<PropertyInfo> ProcessType(Type type)
{
return ProcessType(type, new List<Type>());
}
public static List<PropertyInfo> ProcessType(Type type, List<Type> processedTypes)
{
// Keep track of results
var result = new List<PropertyInfo>();
// Iterate properties of the type
foreach (var property in type.GetProperties())
{
var propertyType = property.PropertyType;
// If the property has a primitive type
if (propertyType.IsPrimitive)
{
// add it to the results
result.Add(property);
}
// If the property has a non-primitive type
// and it has not been processed yet
else if (!processedTypes.Contains(propertyType))
{
// Mark the property's type as already processed
processedTypes.Add(propertyType);
// Recursively processproperties of the property's type
result.AddRange(ProcessType(propertyType, processedTypes));
}
}
return result;
}