如何让所有的原始类的一个对象 [英] how to get all primitive type of an object

查看:154
本文介绍了如何让所有的原始类的一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有具有基本类型对象的所有属性,如果对象有一个关系到另一个类,我想有这样的另一类太



的基本属性

问题是,如果实体A有实体b和b都有A,我该怎么办



在简单的情况下,通过使用反射,我可以得到第一级基本属性,但,我不能进入实体b,并再次获得A的基本属性,,一个循环将被创建,,什么是乌拉圭回合的报价?

 大众A级
{
公共字符串名称{;设置;}
公共BB {获取;集;}
}


公共类b
{
公共字符串等级{获取;集;}
公共AA {获取;集;}
}


解决方案

您可以继续访问类型的轨道,以避免递归:

 大众A级
{
公共字符串名称{;组; }
公众B B {搞定;组; }
}

公共类B
{
公共字符串等级{搞定;组; }
公开发行A A {搞定;组; }
}

类节目
{
静态无效的主要()
{
VAR的结果=访问(typeof运算(A));
的foreach(在结果VAR项)
{
Console.WriteLine(item.Name);
}
}

公共静态的IEnumerable<&的PropertyInfo GT;参观(T型)
{
变种visitedTypes =新的HashSet<类型和GT;();
VAR的结果=新的List<&的PropertyInfo GT;();
InternalVisit(T,visitedTypes,结果);
返回结果;
}

私人无效InternalVisit(T型,HashSet的<类型> visitedTypes,IList的<的PropertyInfo>结果)
{
如果(visitedTypes.Contains(T))
{
的回报;
}

如果
{
visitedTypes.Add(T)(IsPrimitive(T)!);
的foreach(在t.GetProperties VAR财产())
{
如果(IsPrimitive(property.PropertyType))
{
result.Add(财产);
}
InternalVisit(property.PropertyType,visitedTypes,结果);
}
}
}

私人静态布尔IsPrimitive(T型)
{
// TODO:将任何类型的在这里,你认为是原始的,因为我没有
//不太了解你的基本类型的定义是什么
返回新的[] {
typeof运算(字符串),
typeof运算(炭),
typeof运算(字节),
typeof运算(为sbyte),
typeof运算(USHORT),
的typeof(短),
的typeof(UINT),
的typeof (INT),
typeof运算(ULONG),
typeof运算(长),
typeof运算(浮点),
typeof运算(双),
的typeof(十进制),
的typeof(DateTime的),
}。载有(T);
}
}


I wanna have all properties of an object which have primitive type, and if the object has a relation to another class , I wanna have the primitive properties of this another class too

the problem is if entity A has entity B and B has A, what can I do

in simple case by using reflection I can get first level Primitive properties but, I cant go into entity B and again get primitive properties of A,, a loop would be created,, what is ur offer?

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;}
}

解决方案

You could keep a track of visited types to avoid recursion:

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);
    }
}

这篇关于如何让所有的原始类的一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆