Exemplo n.º 1
0
        public void BuildSetter_StandardType_BuildsSetters()
        {
            var flags           = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
            var now             = DateTime.Now;
            var propertySetter1 = DelegateBuilder.BuildSetter <DelegateBuilderTest>(GetType().GetProperty("Property1", flags));

            propertySetter1(this, (long?)1);
            var propertySetter2 = DelegateBuilder.BuildSetter <DelegateBuilderTest>(GetType().GetProperty("Property2", flags));

            propertySetter2(this, (long)2);
            var propertySetter3 = DelegateBuilder.BuildSetter <DelegateBuilderTest>(GetType().GetProperty("Property3", flags));

            propertySetter3(this, now);
            var propertySetter4 = DelegateBuilder.BuildSetter <DelegateBuilderTest>(GetType().GetProperty("Property4", flags));

            propertySetter4(this, now);
            var propertySetter5 = DelegateBuilder.BuildSetter <DelegateBuilderTest>(GetType().GetProperty("Property5", flags));

            propertySetter5(this, "str");
            var propertySetter6 = DelegateBuilder.BuildSetter <DelegateBuilderTest>(GetType().GetProperty("Property6", flags));

            propertySetter6(this, SomeEnum.Value1);
            var propertySetter7 = DelegateBuilder.BuildSetter <DelegateBuilderTest>(GetType().GetProperty("Property7", flags));

            propertySetter7(this, SomeEnum.Value2);

            Property1.Should().Be.EqualTo(1);
            Property2.Should().Be.EqualTo(2);
            Property3.Should().Be.EqualTo(now);
            Property4.Should().Be.EqualTo(now);
            Property5.Should().Be.EqualTo("str");
            Property6.Should().Be.EqualTo(SomeEnum.Value1);
            Property7.Should().Be.EqualTo(SomeEnum.Value2);
        }
Exemplo n.º 2
0
        public IPropertyMapping ToPropertyMapping()
        {
            var arguments = new PropertyMappingArguments <T>
            {
                PropertyName        = PropertyInfo.Name,
                PropertyType        = PropertyInfo.PropertyType,
                AttributeName       = AttributeName ?? PropertyInfo.Name.Replace('_', '-'),
                Getter              = DelegateBuilder.BuildGetter <T>(PropertyInfo),
                Setter              = DelegateBuilder.BuildSetter <T>(PropertyInfo),
                IsDistinguishedName = IsDistinguishedName,
                ReadOnly            = IsDistinguishedName ? Mapping.ReadOnly.Always : ReadOnlyConfiguration.GetValueOrDefault(Mapping.ReadOnly.Never)
            };

            return(new CustomPropertyMapping <T, TProperty>(arguments, _convertFrom, _convertTo, _convertToFilter, _isEqual));
        }
Exemplo n.º 3
0
        public IPropertyMapping ToPropertyMapping()
        {
            var arguments = new PropertyMappingArguments <T>
            {
                PropertyName        = PropertyInfo.Name,
                PropertyType        = PropertyInfo.PropertyType,
                AttributeName       = AttributeName ?? PropertyInfo.Name.Replace('_', '-'),
                Getter              = DelegateBuilder.BuildGetter <T>(PropertyInfo),
                Setter              = DelegateBuilder.BuildSetter <T>(PropertyInfo),
                IsStoreGenerated    = IsStoreGenerated,
                IsDistinguishedName = IsDistinguishedName,
                IsReadOnly          = IsReadOnly,
            };

            return(new CustomPropertyMapping <T, TProperty>(arguments, _convertFrom, _convertTo, _convertToFilter, _isEqual));
        }
Exemplo n.º 4
0
        public void DelegateBuilder_PropertySetter_Speed()
        {
            Speed  inst         = new Speed();
            long   seven        = 7;
            double eight        = 8;
            float  nine         = 9;
            var    prop1Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property1"));
            var    prop2Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property2"));
            var    prop3Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property3"));
            var    prop4Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property4"));
            var    prop5Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property5"));
            var    prop6Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property6"));
            var    prop7Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property7"));
            var    prop8Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property8"));
            var    prop9Setter  = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property9"));
            var    prop10Setter = DelegateBuilder.BuildSetter <Speed>(typeof(Speed).GetProperty("Property10"));
            var    watch        = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                prop1Setter(inst, "1");
                prop2Setter(inst, 2);
                prop3Setter(inst, 3m);
                prop4Setter(inst, new byte[0]);
                prop5Setter(inst, "5");
                prop6Setter(inst, 6);
                prop7Setter(inst, seven);
                prop8Setter(inst, eight);
                prop9Setter(inst, nine);
                prop10Setter(inst, Speed.SpeedEnum.None);
            }
            watch.Stop();
            inst.Property1.Should().Be.EqualTo("1");
            inst.Property2.Should().Be.EqualTo(2);
            inst.Property3.Should().Be.EqualTo(3m);
            inst.Property4.Should().Not.Be.Null();
            inst.Property5.Should().Be.EqualTo("5");
            inst.Property6.Should().Be.EqualTo(6);
            inst.Property7.Should().Be.EqualTo(7);
            inst.Property8.Should().Be.EqualTo(8);
            inst.Property9.Should().Be.EqualTo(9);
            inst.Property10.Should().Be.EqualTo(Speed.SpeedEnum.None);
            Trace.WriteLine(watch.ElapsedMilliseconds);
        }
        public IPropertyMapping ToPropertyMapping()
        {
            var type      = typeof(T);
            var arguments = new PropertyMappingArguments <T>
            {
                PropertyName  = PropertyInfo.Name,
                PropertyType  = PropertyInfo.PropertyType,
                AttributeName = AttributeName ?? PropertyInfo.Name.Replace('_', '-'),
                Getter        = DelegateBuilder.BuildGetter <T>(PropertyInfo),
                Setter        = !type.IsAnonymous()
                             ? DelegateBuilder.BuildSetter <T>(PropertyInfo)
                             : null,
                IsDistinguishedName = IsDistinguishedName,
                ReadOnly            = ReadOnlyConfiguration.GetValueOrDefault(ReadOnly.Never),
                DirectoryMappings   = _directoryMappings,
                InstanceMappings    = _instanceMappings
            };

            var mapping = new DatePropertyMapping <T>(arguments, DateTimeFormat);

            return(mapping);
        }
Exemplo n.º 6
0
        public virtual IPropertyMapping ToPropertyMapping()
        {
            IPropertyMapping mapping;

            var type      = typeof(T);
            var arguments = new PropertyMappingArguments <T>
            {
                PropertyName  = PropertyInfo.Name,
                PropertyType  = PropertyInfo.PropertyType,
                AttributeName = AttributeName ?? PropertyInfo.Name.Replace('_', '-'),
                Getter        = DelegateBuilder.BuildGetter <T>(PropertyInfo),
                Setter        = !type.IsAnonymous()
                                                 ? DelegateBuilder.BuildSetter <T>(PropertyInfo)
                                                 : null,
                IsDistinguishedName = IsDistinguishedName,
                ReadOnly            = IsDistinguishedName ? ReadOnly.Always : ReadOnlyConfiguration.GetValueOrDefault(ReadOnly.Never),
                DirectoryMappings   = null,
                InstanceMappings    = null
            };

            if (PropertyInfo.PropertyType == typeof(DateTime) || PropertyInfo.PropertyType == typeof(DateTime?))
            {
                mapping = new DatePropertyMapping <T>(arguments, DateTimeFormat);
            }
            else if (PropertyInfo.PropertyType.IsEnum || (Nullable.GetUnderlyingType(PropertyInfo.PropertyType) != null &&
                                                          Nullable.GetUnderlyingType(PropertyInfo.PropertyType).IsEnum))
            {
                mapping = new EnumPropertyMapping <T>(arguments, IsEnumStoredAsInt);
            }
            else if (PropertyInfo.PropertyType == typeof(byte[]))
            {
                mapping = new ByteArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(Guid) || PropertyInfo.PropertyType == typeof(Guid?))
            {
                mapping = new GuidPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(string))
            {
                mapping = new StringPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(string[]))
            {
                mapping = new StringArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(DateTime[]) || PropertyInfo.PropertyType == typeof(DateTime?[]))
            {
                mapping = new DateArrayPropertyMapping <T>(arguments, DateTimeFormat);
            }
            else if (PropertyInfo.PropertyType == typeof(ICollection <DateTime>) || PropertyInfo.PropertyType == typeof(Collection <DateTime>) ||
                     PropertyInfo.PropertyType == typeof(ICollection <DateTime?>) || PropertyInfo.PropertyType == typeof(Collection <DateTime?>))
            {
                mapping = new DateCollectionPropertyMapping <T>(arguments, DateTimeFormat);
            }
            else if (PropertyInfo.PropertyType == typeof(Collection <string>) || PropertyInfo.PropertyType == typeof(ICollection <string>))
            {
                mapping = new StringCollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(Collection <byte[]>) || PropertyInfo.PropertyType == typeof(ICollection <byte[]>))
            {
                mapping = new ByteArrayCollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(bool) || PropertyInfo.PropertyType == typeof(bool?))
            {
                mapping = new BooleanPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(SecurityIdentifier))
            {
                mapping = new SecurityIdentifierPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(SecurityIdentifier[]))
            {
                mapping = new SecurityIdentifierArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(ICollection <SecurityIdentifier>) || PropertyInfo.PropertyType == typeof(Collection <SecurityIdentifier>))
            {
                mapping = new SecurityIdentifierCollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(X509Certificate2) || PropertyInfo.PropertyType == typeof(X509Certificate))
            {
                mapping = new X509Certificate2PropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(byte[][]))
            {
                mapping = new ByteArrayArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(ICollection <X509Certificate>) || PropertyInfo.PropertyType == typeof(Collection <X509Certificate>) ||
                     PropertyInfo.PropertyType == typeof(ICollection <X509Certificate2>) || PropertyInfo.PropertyType == typeof(Collection <X509Certificate2>))
            {
                mapping = new X509Certificate2CollectionPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType == typeof(X509Certificate[]) || PropertyInfo.PropertyType == typeof(X509Certificate2[]))
            {
                mapping = new X509Certificate2ArrayPropertyMapping <T>(arguments);
            }
            else if (PropertyInfo.PropertyType.IsValueType || (Nullable.GetUnderlyingType(PropertyInfo.PropertyType) != null))
            {
                mapping = new NumericPropertyMapping <T>(arguments);
            }
            else if (typeof(IDirectoryAttributes).IsAssignableFrom(PropertyInfo.PropertyType))
            {
                mapping = new CatchAllPropertyMapping <T>(arguments);
            }
            else
            {
                throw new MappingException(string.Format("Type '{0}' could not be mapped.", PropertyInfo.PropertyType.FullName));
            }

            return(mapping);
        }