GeneratorDriver.cs :  » Database » NBearLite » NBear » Mapping » 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 » NBear » Mapping » GeneratorDriver.cs
using System;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;

namespace NBear.Mapping{
    /// <summary>
    /// convert handler
    /// </summary>
    /// <param name="inputObject"></param>
    /// <param name="outputObject"></param>
    /// <returns></returns>
    public delegate object ConvertHandler(object inputObject, object outputObject);

    sealed class ConvertorGeneratorHelper
    {
        public static string GetCurrentKey(Dictionary<int, string> mappingOrders, int currentCount, List<string> keyList)
        {
            string key = null;
            //
            foreach (string k in keyList)
            {
                if (!mappingOrders.ContainsValue(k))
                {
                    key = k;
                    break;
                }
            }
            if (mappingOrders.ContainsKey(currentCount))
            {
                key = mappingOrders[currentCount];
            }
            keyList.Remove(key);
            return key;
        }

        public static Type GetMemberType(MemberInfo member)
        {
            Type type = null;
            if (member.MemberType == MemberTypes.Field)
            {
                type = ((FieldInfo)member).FieldType;
            }
            else
            {
                type = ((PropertyInfo)member).PropertyType;
            }
            return type;
        }

        public static Type GetOriginalType(Type type)
        {
            Type originalType = type.IsEnum ? typeof(int) : type;
            originalType = CommonUtils.GetOriginalTypeOfNullableType(originalType);
            return originalType;
        }

        public static Dictionary<string, MemberInfo> GetMembers(Type type)
        {
            Dictionary<string, MemberInfo> members = new Dictionary<string, MemberInfo>();
            Type objType = type;
            while (objType != null && objType != typeof(Object) && objType != typeof(ValueType))
            {
                foreach (PropertyInfo property in objType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    if (property.CanRead && property.CanWrite)
                    {
                        members.Add(property.Name, property);
                    }
                }
                foreach (FieldInfo field in objType.GetFields(BindingFlags.Instance | BindingFlags.Public))
                {
                    if (!field.IsInitOnly)
                    {
                        members.Add(field.Name, field);
                    }
                }
                objType = objType.BaseType;
            }
            return members;
        }

        public static MemberInfo GetMemberInfo(Type type, string name)
        {
            MemberInfo member = type.GetProperty(name);
            if (member == null)
            {
                member = type.GetField(name);
            }
            return member;
        }

        public static Dictionary<TValue, TKey> RevertDictionary<TKey, TValue>(Dictionary<TKey, TValue> dictionary)
        {
            Dictionary<TValue, TKey> dic = new Dictionary<TValue, TKey>();
            foreach (TKey key in dictionary.Keys)
            {
                dic.Add(dictionary[key], key);
            }
            return dic;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public abstract class GeneratorDriver
    {
        public GeneratorDriver() { }
        private IMemberGetterGenerator memberGetterGenerator;
        public IMemberGetterGenerator MemberGetterGenerator
        {
            get { return memberGetterGenerator; }
        }
        private IMemberSetterGenerator memberSetterGenerator;
        public IMemberSetterGenerator MemberSetterGenerator
        {
            get { return memberSetterGenerator; }
        }

        public GeneratorDriver(IMemberGetterGenerator memberGetterGenerator, IMemberSetterGenerator memberSetterGenerator)
        {
            this.memberGetterGenerator = memberGetterGenerator;
            this.memberSetterGenerator = memberSetterGenerator;
        }

        public abstract ConvertHandler GetConvertHandler(Type inputType, Type outputType, object inputObject, object outputObject, Dictionary<string, string> mappingNames, List<string> ignoreList, Dictionary<int, string> mappingOrders, bool mappingSpecifiedOnly);
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.