Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
943 views
in Technique[技术] by (71.8m points)

c# - how to get all primitive type of an object

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;}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...