/// <summary>
        /// Prepares the entity serialization configuration to be bound to the Xml serializer.
        /// </summary>
        /// <param name="context">Xml serializer context that the entity will be bound to.</param>
        internal void ConfigureEntity(SerializerConfigurationContext context)
        {
            var buildContext = new ConfigurationBuildContext();

            foreach (var p in this.PropertyConfigurations)
            {
                p.BuildConfiguration(buildContext);
            }

            if (false == buildContext.ProxyProperties.Any())
            {
                return;
            }

            var moduleBuilder    = ProxyModuleBuilder.GetInstance(this._contextType);
            var proxyImplementer = ProxyTypeImplementer.GetInstance(this.EntityType, moduleBuilder);
            var proxyProperties  = this.EntityType.GetProperties()                                                       // Get all of the properties from the entity
                                   .Where(e => e.CanRead && e.CanWrite)                                                  // That have setters and getters
                                   .Where(e => false == buildContext.ProxyProperties.Any(i => i.PropertyName == e.Name)) // That are not overridden by one of the property configs
                                   .Select(this.CreateProxyProperty)                                                     // And build up a proxy property info object for each
                                   .Union(buildContext.ProxyProperties)                                                  // Combine these with the overridden proxy properties
                                   .ToArray();

            foreach (var p in proxyProperties)
            {
                proxyImplementer.AddPropertyInfo(p);
            }

            var proxyType = proxyImplementer.BuildProxyType();

            context.ProxyTypes.Add(this.EntityType, proxyType);
        }
Exemple #2
0
        public void TestProxyGenerator()
        {
            try
            {
                var moduleBuilder = ProxyModuleBuilder.GetInstance <XmlSerializerContextTests>();
                var pg            = ProxyTypeImplementer.GetInstance <PoLine>(moduleBuilder);
                var pi            = new ProxyPropertyInfo
                {
                    PropertyName                = "Tax",
                    BasePropertyInfo            = typeof(PoLine).GetProperty("Tax"),
                    ProxyPropertyType           = typeof(string),
                    ConversionToProxyDelegate   = ((MethodCallExpression)((Expression <Func <decimal, string> >)(d => NullableDecimalToString(d))).Body).Method,
                    ConversionFromProxyDelegate = ((MethodCallExpression)((Expression <Func <string, decimal?> >)(s => ParseToNullableDecimal(s))).Body).Method
                };
                pg.AddPropertyInfo(pi);
                var t  = pg.BuildProxyType();
                var i  = Activator.CreateInstance(t);
                var tp = t.GetProperty("Tax");

                var ov = tp.GetValue(i);
                // tp.SetValue(i, new Nullable<decimal>(1107.0m));
                tp.SetValue(i, "1107.0");
                // var op0 = t.GetMethod("op_Explicit", new[] { t });
                var op0 = t.GetMethod("op_Implicit", new[] { t });
                // var x = op0.Invoke(null, new[] { i });
                // var op1 = t.GetMethod("op_Explicit", new[] { typeof(PoLine) });
                var op1 = t.GetMethod("op_Implicit", new[] { typeof(PoLine) });
                var x   = new PoLine {
                    Tax = 33.0m
                };
                var y = op1.Invoke(null, new[] { x });
                var z = new PoLine[1];

                var bi = (PoLine)i;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemple #3
0
 /// <summary>
 ///     Returns the <see cref="System.Reflection.Emit.ModuleBuilder"/> where the new
 ///     type will be defined. Override to add the type to another module.
 /// </summary>
 /// <returns>
 ///     Returns the <see cref="System.Reflection.Emit.ModuleBuilder"/> the defined
 ///     type will be added to.
 /// </returns>
 protected virtual ModuleBuilder GetModuleBuilder()
 {
     return(ProxyModuleBuilder.Get());
 }