コード例 #1
0
        public void DelegateBuilder_PropertyGetter_Speed()
        {
            Speed  inst          = new Speed();
            long   seven         = 7;
            double eight         = 8;
            float  nine          = 9;
            var    prop1Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property1"));
            var    prop2Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property2"));
            var    prop3Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property3"));
            var    prop4Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property4"));
            var    prop5Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property5"));
            var    prop6Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property6"));
            var    prop7Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property7"));
            var    prop8Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property8"));
            var    prop9Getter   = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property9"));
            var    prop10GGetter = DelegateBuilder.BuildGetter <Speed>(typeof(Speed).GetProperty("Property10"));

            inst.Property1  = "1";
            inst.Property2  = 2;
            inst.Property3  = 3m;
            inst.Property4  = new byte[0];
            inst.Property5  = "5";
            inst.Property6  = 6;
            inst.Property7  = seven;
            inst.Property8  = eight;
            inst.Property9  = nine;
            inst.Property10 = Speed.SpeedEnum.None;
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                var a = prop1Getter(inst);
                var b = prop2Getter(inst);
                var c = prop3Getter(inst);
                var d = prop4Getter(inst);
                var e = prop5Getter(inst);
                var f = prop6Getter(inst);
                var g = prop7Getter(inst);
                var h = prop8Getter(inst);
                var j = prop9Getter(inst);
                var k = prop10GGetter(inst);
            }
            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);
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        public void BuildGetter_StandardType_BuildsGetters()
        {
            Property1 = 1;
            Property2 = 2;
            Property3 = DateTime.Now;
            Property4 = DateTime.Now;
            Property5 = "str";
            Property6 = SomeEnum.Value1;
            Property7 = SomeEnum.Value2;

            int count = 0;

            foreach (var property in GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                var getter = DelegateBuilder.BuildGetter <DelegateBuilderTest>(property);
                var value = getter(this);
                value.Should().Not.Be.Null();
                value.Should().Be.EqualTo(property.GetValue(this, null));
                count++;
            }

            count.Should().Be.EqualTo(7);
        }
コード例 #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);
        }
コード例 #7
0
 private static Func <T, object> GetGetter <T>(T example, PropertyInfo propertyInfo)
 {
     return(DelegateBuilder.BuildGetter <T>(propertyInfo));
 }