ObjectGetterGenerator.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 » ObjectGetterGenerator.cs
using System;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
namespace NBear.Mapping{
    public class ObjectGetterGenerator : GeneratorDriver, IMemberGetterGenerator
    {
        /// <summary>
        ///   
        /// </summary>
        public ObjectGetterGenerator() { }

        /// <summary>
        ///   inherite base 
        /// </summary>
        /// <param name="memberGetterGenerator">The member getter generator.</param>
        /// <param name="memberSetterGenerator">The member setter generator.</param>
        public ObjectGetterGenerator(IMemberGetterGenerator memberGetterGenerator, IMemberSetterGenerator memberSetterGenerator)
            : base(memberGetterGenerator, memberSetterGenerator)
        {
        }

        #region IMemberGetterGenerator Members

        public void GetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, ref LocalBuilder memberValue)
        {
            Type inputType = targetObject.ArgType;
            MemberInfo memberInfo = ConvertorGeneratorHelper.GetMemberInfo(inputType, memberName);
            if (memberInfo == null)
            {
                return;
            }
            Type srcMemberType = ConvertorGeneratorHelper.GetMemberType(memberInfo);
            Type destMemberType = memberValue.LocalType;
            if (memberValue.LocalType.IsAssignableFrom(srcMemberType))
            {
                generator.Ldarg(targetObject);
                generator.LoadMember(memberInfo);
                generator.Stloc(memberValue);
                return;
            }

            Type converTargetType = ConvertorGeneratorHelper.GetOriginalType(destMemberType);

            Type convertSourceType = ConvertorGeneratorHelper.GetOriginalType(srcMemberType);

            //IConvertible
            if (typeof(IConvertible).IsAssignableFrom(convertSourceType))
            {
                generator.Ldarg(targetObject);
                generator.LoadMember(memberInfo);
                if (convertSourceType.IsValueType)
                {
                    //
                    generator.Box(srcMemberType);
                }
                generator.Load(converTargetType);
                generator.Call(typeof(Convert).GetMethod("ChangeType", new Type[] { typeof(object), typeof(Type) }));
                if (destMemberType.IsValueType)
                {
                    generator.UnboxAny(destMemberType);
                }
                generator.Stloc(memberValue);
            }
        }

        #endregion

        public override ConvertHandler GetConvertHandler(Type inputType, Type outputType, object inputObject, object outputObject, Dictionary<string, string> mappingNames, List<string> ignoreList, Dictionary<int, string> mappingOrders, bool mappingSpecifiedOnly)
        {
            //MemberInfo
            Dictionary<string, string> distToSrc = new Dictionary<string, string>();
            Dictionary<string, MemberInfo> members = new Dictionary<string, MemberInfo>();
            //
            foreach (string sourceName in mappingNames.Keys)
            {
                MemberInfo sourceMember = ConvertorGeneratorHelper.GetMemberInfo(inputType, sourceName);
                Check.Require(sourceMember != null, string.Format("member named {0} could not be found in {1}", sourceName, outputType.FullName));
                distToSrc.Add(mappingNames[sourceName], sourceName);
                members.Add(mappingNames[sourceName], sourceMember);
            }
            //
            if (!mappingSpecifiedOnly)
            {
                Dictionary<string, MemberInfo> sourceMembers = ConvertorGeneratorHelper.GetMembers(inputType);
                foreach (string sourceName in sourceMembers.Keys)
                {
                    if (!ignoreList.Contains(sourceName) && !distToSrc.ContainsKey(sourceName))
                    {
                        distToSrc.Add(sourceName, sourceName);
                        members.Add(sourceName, sourceMembers[sourceName]);
                    }
                }
            }

            CodeGenerator gen = new CodeGenerator();
            gen.BeginMethod("m" + Guid.NewGuid().ToString("N"), typeof(ConvertHandler));

            ArgBuilder inputObjectArg = new ArgBuilder(0, inputType);
            ArgBuilder outputObjectArg = new ArgBuilder(1, outputType);

            int currentCount = 0;
            int memberCount = members.Count;
            string[] keys = new string[memberCount];
            distToSrc.Keys.CopyTo(keys, 0);
            List<string> keyList = new List<string>(keys);
            MemberSetterGenerator.BeginSetMembers(gen, outputObjectArg);
            //
            while (currentCount < memberCount)
            {
                currentCount++;
                string targetName = ConvertorGeneratorHelper.GetCurrentKey(mappingOrders, currentCount, keyList);
                if (string.IsNullOrEmpty(targetName))
                {
                    continue;
                }
                string sourceName = distToSrc[targetName];
                if (MemberGetterGenerator.ContainsMember(sourceName, inputType, inputObject) && MemberSetterGenerator.ContainsMember(targetName, outputType, outputObject))
                {
                    Type targetType = ConvertorGeneratorHelper.GetMemberType(members[targetName]);

                    LocalBuilder memberValue = gen.DeclareLocal(targetType, "memberValue");

                    MemberGetterGenerator.GetMemberValue(gen, inputObjectArg, sourceName, ref memberValue);

                    MemberSetterGenerator.SetMemberValue(gen, outputObjectArg, targetName, memberValue);
                }
            }
            gen.Ldarg(outputObjectArg);
            MemberSetterGenerator.EndSetMembers(gen, outputObjectArg);
            return (ConvertHandler)gen.EndMethod();
        }

        #region IMemberGetterGenerator Members

        public bool ContainsMember(string name, Type type, object inputObject)
        {
            return ConvertorGeneratorHelper.GetMemberInfo(type, name) != null;
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.