DuckCollectionReflector.cs :  » Development » Jayrock » JsonConversionsDemo » 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 » Development » Jayrock 
Jayrock » JsonConversionsDemo » DuckCollectionReflector.cs
namespace JsonConversionsDemo{
    #region Imports

    using System;
    using System.Diagnostics;
    using System.Reflection;

    #endregion

    /// <summary>
    /// Provides duck-type reflection services for collection-like types.
    /// This means that a type in question does not have to be a real
    /// collection as long as it seems like one.
    /// </summary>

    public sealed class DuckCollectionReflector
    {
        public static Type InferElementType(Type type)
        {
            if (type == null) throw new ArgumentNullException("type");

            MemberInfo[] indexers = type.FindMembers(MemberTypes.Property, 
                BindingFlags.Instance | BindingFlags.Public, IsIndexer, null);

            if (indexers.Length == 0)
                throw new ArgumentException(string.Format("{0} does not appear to have an indexer property.", type.FullName), "type");

            return ((PropertyInfo) indexers[0]).PropertyType;
        }

        private static bool IsIndexer(MemberInfo m, object filterCriteria)
        {
            if (m.Name != "Item")
                return false;

            Debug.Assert(m is PropertyInfo);
            
            PropertyInfo property = (PropertyInfo) m;
            return property.CanRead && property.GetIndexParameters().Length == 1;
        }

        public static MethodInfo FindAddMethod(Type type)
        {
            return FindAddMethod(type, null);
        }

        public static MethodInfo FindAddMethod(Type type, Type elementType)
        {
            if (type == null) throw new ArgumentNullException("type");

            if (elementType == null)
                elementType = InferElementType(type);

            return type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance,
                /* binder */ null, new Type[] { elementType }, /* modifiers */ null);
        }

        public static MethodInfo GetAddMethod(Type type)
        {
            return GetAddMethod(type, null);
        }

        public static MethodInfo GetAddMethod(Type type, Type elementType)
        {
            MethodInfo adder = FindAddMethod(type, elementType);

            if (adder == null)
            {
                throw new ArgumentException(string.Format(
                    "{0} has no public Add method that takes a single {1} argument.",
                    type.FullName, elementType.FullName), "type");
            }

            return adder;
        }

        public static Action<Element> GetAdder<Element>(object collection)
        {
            Action<Element> adder = FindAddder<Element>(collection);

            if (adder == null)
            {
                throw new ArgumentException(string.Format(
                    "{0} has no public Add method that takes a single {1} argument.",
                    collection.GetType().FullName, typeof(Element).FullName), "collection");
            }

            return adder;
        }

        public static Action<Element> FindAddder<Element>(object collection)
        {
            if (collection == null) throw new ArgumentNullException("collection");

            MethodInfo adder = FindAddMethod(collection.GetType(), typeof(Element));

            if (adder == null)
                return null;

            return (Action<Element>) Delegate.CreateDelegate(typeof(Action<Element>), collection, adder);
        }

        private DuckCollectionReflector() {}
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.