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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Reflection » ConstructorInfoScreenshots 
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.
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.