ObjectSetterGenerator.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 » ObjectSetterGenerator.cs
using System;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;

namespace NBear.Mapping{
    /// <summary>
    /// object setter generator
    /// </summary>
    public class ObjectSetterGenerator : GeneratorDriver, IMemberSetterGenerator
    {
        /// <summary>
        ///   
        /// </summary>
        public ObjectSetterGenerator() { }

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

        #region IGeneratorDriver Members

        /// <summary>
        ///   get convert handler
        /// </summary>
        /// <param name="inputType">Type of the input.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <param name="mappingNames">The mapping names.</param>
        /// <param name="ignoreList">The ignore list.</param>
        /// <param name="mappingOrders">The mapping orders.</param>
        /// <param name="mappingSpecifiedOnly">if set to <c>true</c> [mapping specified only].</param>
        /// <returns></returns>
        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 targetMember = ConvertorGeneratorHelper.GetMemberInfo(outputType, mappingNames[sourceName]);
                Check.Require(targetMember != null, string.Format("member named {0} could not be found in {1}", mappingNames[sourceName], outputType.FullName));
                distToSrc.Add(mappingNames[sourceName], sourceName);
                members.Add(mappingNames[sourceName], targetMember);
            }
            //
            if (!mappingSpecifiedOnly)
            {
                Dictionary<string, MemberInfo> targetMembers = ConvertorGeneratorHelper.GetMembers(outputType);
                foreach (string targetName in targetMembers.Keys)
                {
                    if (!ignoreList.Contains(targetName) && !distToSrc.ContainsKey(targetName))
                    {
                        distToSrc.Add(targetName, targetName);
                        members.Add(targetName, targetMembers[targetName]);
                    }
                }
            }

            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();
        }

        #endregion

        #region IMemberSetterGenerator Members

        public void SetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, LocalBuilder memberValue)
        {
            MemberInfo memberInfo = ConvertorGeneratorHelper.GetMemberInfo(targetObject.ArgType, memberName);
            if (memberInfo != null)
            {
                generator.Ldarg(targetObject);
                generator.Ldloc(memberValue);
                generator.StoreMember(memberInfo);
            }
        }

        /// <summary>
        /// Ends the set members.
        /// </summary>
        /// <param name="generator">The generator.</param>
        public void EndSetMembers(CodeGenerator generator)
        {

        }
        #endregion

        #region IMemberSetterGenerator Members

        public void BeginSetMembers(CodeGenerator generator, ArgBuilder targetObject)
        {

        }

        public void EndSetMembers(CodeGenerator generator, ArgBuilder targetObject)
        {

        }

        #endregion

        #region IMemberSetterGenerator Members

        public bool ContainsMember(string name, Type type, object outputObject)
        {
            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.