ReflectionUtils.cs :  » Database » NBearLite » System » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Database » NBearLite 
NBearLite » System » ReflectionUtils.cs
using System;
using System.Collections.Generic;
using System.Reflection;

namespace System{
    /// <summary>
    /// ReflectionUtils
    /// </summary>
    public sealed class ReflectionUtils
    {
        private ReflectionUtils() { }

        /// <summary>
        /// Deeply get properties of specific types
        /// </summary>
        /// <param name="types">The types</param>
        /// <returns>The property infos</returns>
        public static PropertyInfo[] DeepGetProperties(params Type[] types)
        {
            if (types == null || types.Length == 0)
            {
                return new PropertyInfo[0];
            }
            List<PropertyInfo> list = new List<PropertyInfo>();
            foreach (Type t in types)
            {
                if (t != null)
                {
                    foreach (PropertyInfo pi in t.GetProperties())
                    {
                        list.Add(pi);
                    }

                    if (t.IsInterface)
                    {
                        Type[] interfaceTypes = t.GetInterfaces();

                        if (interfaceTypes != null)
                        {
                            foreach (PropertyInfo pi in DeepGetProperties(interfaceTypes))
                            {
                                bool isContained = false;

                                foreach (PropertyInfo item in list)
                                {
                                    if (item.Name == pi.Name)
                                    {
                                        isContained = true;
                                        break;
                                    }
                                }

                                if (!isContained)
                                {
                                    list.Add(pi);
                                }
                            }
                        }
                    }
                    else
                    {
                        Type baseType = t.BaseType;

                        if (baseType != typeof(object) && baseType != typeof(ValueType))
                        {
                            foreach (PropertyInfo pi in DeepGetProperties(baseType))
                            {
                                bool isContained = false;

                                foreach (PropertyInfo item in list)
                                {
                                    if (item.Name == pi.Name)
                                    {
                                        isContained = true;
                                        break;
                                    }
                                }

                                if (!isContained)
                                {
                                    list.Add(pi);
                                }
                            }
                        }
                    }
                }
            }

            return list.ToArray();
        }

        /// <summary>
        /// Deeply get fields of specific fields
        /// </summary>
        /// <param name="types">The types</param>
        /// <returns>The field infos</returns>
        public static FieldInfo[] DeepGetFields(params Type[] types)
        {
            if (types == null || types.Length == 0)
            {
                return new FieldInfo[0];
            }
            List<FieldInfo> list = new List<FieldInfo>();
            foreach (Type t in types)
            {
                if (t != null)
                {
                    foreach (FieldInfo fi in t.GetFields())
                    {
                        list.Add(fi);
                    }

                    if (t.IsInterface)
                    {
                        Type[] interfaceTypes = t.GetInterfaces();

                        if (interfaceTypes != null)
                        {
                            foreach (FieldInfo fi in DeepGetFields(interfaceTypes))
                            {
                                bool isContained = false;

                                foreach (FieldInfo item in list)
                                {
                                    if (item.Name == fi.Name)
                                    {
                                        isContained = true;
                                        break;
                                    }
                                }

                                if (!isContained)
                                {
                                    list.Add(fi);
                                }
                            }
                        }
                    }
                    else
                    {
                        Type baseType = t.BaseType;

                        if (baseType != typeof(object) && baseType != typeof(ValueType))
                        {
                            foreach (FieldInfo fi in DeepGetFields(baseType))
                            {
                                bool isContained = false;

                                foreach (FieldInfo item in list)
                                {
                                    if (item.Name == fi.Name)
                                    {
                                        isContained = true;
                                        break;
                                    }
                                }

                                if (!isContained)
                                {
                                    list.Add(fi);
                                }
                            }
                        }
                    }
                }
            }

            return list.ToArray();
        }

        /// <summary>
        /// Deeply get property info from specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static PropertyInfo DeepGetProperty(Type type, string propertyName)
        {
            foreach (PropertyInfo pi in DeepGetProperties(type))
            {
                if (pi.Name == propertyName)
                {
                    return pi;
                }
            }

            return null;
        }

        /// <summary>
        /// Deeps the get field from specific type.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <param name="includePublic"></param>
        /// <param name="includeNonPublic"></param>
        /// <param name="isStatic"></param>
        /// <returns></returns>
        public static FieldInfo DeepGetField(Type type, string name, bool includePublic, bool includeNonPublic, bool isStatic)
        {
            Type t = type;
            if (t != null)
            {
                BindingFlags flags = BindingFlags.Instance;
                if (includePublic)
                    flags |= BindingFlags.Public;
                if (includeNonPublic)
                    flags |= BindingFlags.NonPublic;
                if (isStatic)
                    flags |= BindingFlags.Static;
                FieldInfo fi = t.GetField(name, flags);
                if (fi != null)
                {
                    return fi;
                }

                if (t.IsInterface)
                {
                    Type[] interfaceTypes = t.GetInterfaces();

                    if (interfaceTypes != null)
                    {
                        foreach (Type interfaceType in interfaceTypes)
                        {
                            fi = DeepGetField(interfaceType, name, includePublic, includeNonPublic, isStatic);
                            if (fi != null)
                            {
                                return fi;
                            }
                        }
                    }
                }
                else
                {
                    Type baseType = t.BaseType;

                    if (baseType != typeof(object) && baseType != typeof(ValueType))
                    {
                        return DeepGetField(baseType, name, includePublic, includeNonPublic, isStatic);
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// GetMethodInfoFromArrayBySignature
        /// </summary>
        /// <param name="signature">The signature</param>
        /// <param name="mis">The method info array</param>
        /// <returns>The method info</returns>
        public static MethodInfo GetMethodInfoFromArrayBySignature(string signature, MethodInfo[] mis)
        {
            if (mis == null)
            {
                return null;
            }

            foreach (MethodInfo mi in mis)
            {
                if (mi.ToString() == signature)
                {
                    return mi;
                }
            }

            return null;
        }

        /// <summary>
        /// GetMethodInfoBySignature
        /// </summary>
        /// <param name="type"></param>
        /// <param name="signature"></param>
        /// <param name="includePublic"></param>
        /// <param name="includeNonPublic"></param>
        /// <param name="isStatic"></param>
        /// <returns></returns>
        public static MethodInfo GetMethodInfoBySignature(Type type, string signature, bool includePublic, bool includeNonPublic, bool isStatic)
        {
            Check.Require(type, "type");

            BindingFlags flags = BindingFlags.Instance;
            if (includePublic)
                flags |= BindingFlags.Public;
            if (includeNonPublic)
                flags |= BindingFlags.NonPublic;
            if (isStatic)
                flags |= BindingFlags.Static;
            return GetMethodInfoFromArrayBySignature(signature, type.GetMethods(flags));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.