Get Property/Constructor Info : ConstructorInfo « Reflection « C# / C Sharp






Get Property/Constructor Info

        
using System;
using System.Collections.Generic;
using System.Reflection;

namespace EasyMapping.Common
{
    public class ReflectionCache
    {
        private static Dictionary<string, PropertyInfo[]> _propertyInfoCache = new Dictionary<string, PropertyInfo[]>();
        private static Dictionary<string, ConstructorInfo> _constructorInfoCache = new Dictionary<string, ConstructorInfo>();
        private static  volatile object SyncRoot = new object(); 

        public static PropertyInfo[] GetPropertyInfo(Type type)
        {
            if (type == null)
            {
                throw new ArgumentException("Parameter should not be null");
            }
            PropertyInfo[] target;
            if (_propertyInfoCache.TryGetValue(type.FullName, out target))
            {
                return target;
            }
            else 
            {
                lock (SyncRoot)
                {
                    PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    _propertyInfoCache.Add(type.FullName, properties);
                    target = properties;
                }
                return target;
            }         
        }

        public static ConstructorInfo GetConstructorInfo(Type type)
        {
            if (type == null)
            {
                throw new ArgumentException("Parameter should not be null");
            }
            ConstructorInfo target;
            if (_constructorInfoCache.TryGetValue(type.FullName, out target))
            {
                return target;
            }
            else
            {
                lock (SyncRoot)
                {
                    ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
                    if (constructor == null)
                    {
                        foreach (var item in type.GetConstructors(BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance))
                        {
                            if (item != null)
                            {
                                constructor = item;
                                break;
                            }
                        }
                    }
                    _constructorInfoCache.Add(type.FullName, constructor);
                    target = constructor;
                }
                return target;
            }
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Call GetConstructor to get the constructor
2.Invoke in Constructor through ConstructorInfo
3.Get parameter information by using ConstructorInfo and ParameterInfo
4.ConstructorInfo Class Discovers the attributes of a class constructor
5.Searches for a constructor whose parameters match the specified argument types and modifiers
6.Searches for a constructor using the specified binding constraints.
7.Get the constructor that takes an integer as a parameter.
8.Returns all the public constructors defined for the current Type.