Пример #1
0
        public void CreateGetField()
        {
            TestClass.IsStatic   = true;
            TestClass.StaticName = "CreateGetProperty";
            var testClass = new TestClass
            {
                Age        = 21,
                FirstName  = "John",
                Internal   = "test",
                LastName   = "Doe",
                PrivateGet = "Private Get"
            };

            var       fieldInfos = typeof(TestClass).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
            Stopwatch watch      = Stopwatch.StartNew();

            foreach (var fieldInfo in fieldInfos)
            {
                var d = DelegateFactory.CreateGet(fieldInfo);
                Assert.IsNotNull(d);

                var value = d(testClass);
                Assert.IsNotNull(value);

                Console.WriteLine("Field: {0} Value: {1}", fieldInfo.Name, value);
            }
            watch.Stop();
            Console.WriteLine("Time: {0}ms", watch.ElapsedMilliseconds);
        }
Пример #2
0
 public MethodGetter(MethodInfo methodInfo)
 {
     _methodInfo      = methodInfo;
     _name            = _methodInfo.Name;
     _memberType      = _methodInfo.ReturnType;
     _lateBoundMethod = DelegateFactory.CreateGet(methodInfo);
 }
Пример #3
0
        void Write(StringBuilder builder, Type t, object obj)
        {
            if (obj == null)
            {
                // For null entries in a nullable array
                // See https://github.com/Particular/NServiceBus/issues/2706
                if (t.IsNullableType())
                {
                    builder.Append("null");
                }

                return;
            }

            IEnumerable <PropertyInfo> properties;

            if (!cache.typeToProperties.TryGetValue(t, out properties))
            {
                throw new InvalidOperationException(string.Format("Type {0} was not registered in the serializer. Check that it appears in the list of configured assemblies/types to scan.", t.FullName));
            }

            foreach (var prop in properties)
            {
                if (IsIndexedProperty(prop))
                {
                    throw new NotSupportedException(string.Format("Type {0} contains an indexed property named {1}. Indexed properties are not supported on message types.", t.FullName, prop.Name));
                }
                WriteEntry(prop.Name, prop.PropertyType, DelegateFactory.CreateGet(prop).Invoke(obj), builder);
            }

            foreach (var field in cache.typeToFields[t])
            {
                WriteEntry(field.Name, field.FieldType, DelegateFactory.CreateGet(field).Invoke(obj), builder);
            }
        }
Пример #4
0
        void Write(StringBuilder builder, Type t, object obj)
        {
            if (obj == null)
            {
                return;
            }

            IEnumerable <PropertyInfo> properties;

            if (!typeToProperties.TryGetValue(t, out properties))
            {
                throw new InvalidOperationException(string.Format("Type {0} was not registered in the serializer. Check that it appears in the list of configured assemblies/types to scan.", t.FullName));
            }

            foreach (var prop in properties)
            {
                if (IsIndexedProperty(prop))
                {
                    throw new NotSupportedException(string.Format("Type {0} contains an indexed property named {1}. Indexed properties are not supported on message types.", t.FullName, prop.Name));
                }
                WriteEntry(prop.Name, prop.PropertyType, DelegateFactory.CreateGet(prop).Invoke(obj), builder);
            }

            foreach (var field in typeToFields[t])
            {
                WriteEntry(field.Name, field.FieldType, DelegateFactory.CreateGet(field).Invoke(obj), builder);
            }
        }
Пример #5
0
        public static object GetValue(this MemberInfo member, object source)
        {
            var fieldInfo = member as FieldInfo;

            if (fieldInfo != null)
            {
                var field = DelegateFactory.CreateGet(fieldInfo);
                return(field.Invoke(source));
            }

            var propertyInfo = (PropertyInfo)member;

            if (!propertyInfo.CanRead)
            {
                if (propertyInfo.PropertyType.IsValueType)
                {
                    return(Activator.CreateInstance(propertyInfo.PropertyType));
                }

                return(null);
            }

            var property = DelegateFactory.CreateGet(propertyInfo);

            return(property.Invoke(source));
        }
Пример #6
0
 public FieldGetter(FieldInfo fieldInfo)
 {
     _fieldInfo         = fieldInfo;
     _name              = fieldInfo.Name;
     _memberType        = fieldInfo.FieldType;
     _lateBoundFieldGet = DelegateFactory.CreateGet(fieldInfo);
 }
Пример #7
0
        public void CreateGetPropertyValueType()
        {
            StructureClass.IsStatic   = true;
            StructureClass.StaticName = "CreateGetProperty";
            var testClass = new StructureClass
            {
                Age        = 21,
                FirstName  = "John",
                Internal   = "test",
                LastName   = "Doe",
                PrivateGet = "Private Get"
            };

            testClass.Defaults();

            var       properties = typeof(StructureClass).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
            Stopwatch watch      = Stopwatch.StartNew();

            foreach (var propertyInfo in properties)
            {
                var d = DelegateFactory.CreateGet(propertyInfo);
                Assert.IsNotNull(d);

                var value = d(testClass);
                Assert.IsNotNull(value);

                Console.WriteLine("Property: {0} Value: {1}", propertyInfo.Name, value);
            }
            watch.Stop();
            Console.WriteLine("Time: {0}ms", watch.ElapsedMilliseconds);
        }
        public FieldAccessor(FieldInfo fieldInfo) : base(fieldInfo.Name, fieldInfo.FieldType)
        {
            _fieldInfo  = fieldInfo;
            _getHandler = DelegateFactory.CreateGet(fieldInfo);
            _setHandler = DelegateFactory.CreateSet(fieldInfo);

            _isInternal = _fieldInfo.IsPrivate || _fieldInfo.IsFamily;
        }
Пример #9
0
 public PropertyGetter(PropertyInfo propertyInfo)
 {
     _propertyInfo = propertyInfo;
     _name         = _propertyInfo.Name;
     _memberType   = _propertyInfo.PropertyType;
     if (_propertyInfo.GetGetMethod(true) != null)
     {
         _lateBoundPropertyGet = DelegateFactory.CreateGet(propertyInfo);
     }
 }
Пример #10
0
        public void MethodTests()
        {
            MethodInfo      method   = typeof(String).GetMethod("StartsWith", new[] { typeof(string) });
            LateBoundMethod callback = DelegateFactory.CreateGet(method);

            string foo    = "this is a test";
            bool   result = (bool)callback(foo, new[] { "this" });

            result.ShouldBeTrue();
        }
Пример #11
0
        public void FieldTests()
        {
            FieldInfo         field    = typeof(Source).GetField("Value2");
            LateBoundFieldGet callback = DelegateFactory.CreateGet(field);

            var source = new Source {
                Value2 = 15
            };
            int result = (int)callback(source);

            result.ShouldEqual(15);
        }
Пример #12
0
        public void PropertyTests()
        {
            PropertyInfo         property = typeof(Source).GetProperty("Value", typeof(int));
            LateBoundPropertyGet callback = DelegateFactory.CreateGet(property);

            var source = new Source {
                Value = 5
            };
            int result = (int)callback(source);

            result.ShouldEqual(5);
        }
Пример #13
0
        private Dictionary <PropertyInfo, LateBoundPropertyGet> BuildPropertyValueGetFunctions()
        {
            if (_properties == null)
            {
                _properties = GetProperties();
            }
            var lateBondPropertyGetList = new Dictionary <PropertyInfo, LateBoundPropertyGet>(_properties.Count);

            foreach (var propertyInfo in _properties)
            {
                lateBondPropertyGetList.Add(propertyInfo, DelegateFactory.CreateGet(propertyInfo));
            }
            return(lateBondPropertyGetList);
        }
Пример #14
0
    /// <summary>
    /// Initializes a new instance of the <see cref="FieldAccessor"/> class.
    /// </summary>
    /// <param name="fieldInfo">The <see cref="FieldInfo"/> instance to use for this accessor.</param>
    public FieldAccessor(FieldInfo fieldInfo)
    {
        _fieldInfo  = fieldInfo;
        _name       = fieldInfo.Name;
        _memberType = fieldInfo.FieldType;

        _hasGetter    = true;
        _lateBoundGet = new Lazy <LateBoundGet>(() => DelegateFactory.CreateGet(_fieldInfo));

        _hasSetter = !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral;
        if (_hasSetter)
        {
            _lateBoundSet = new Lazy <LateBoundSet>(() => DelegateFactory.CreateSet(_fieldInfo));
        }
    }
Пример #15
0
    /// <summary>
    /// Initializes a new instance of the <see cref="PropertyAccessor"/> class.
    /// </summary>
    /// <param name="propertyInfo">The <see cref="PropertyInfo"/> instance to use for this accessor.</param>
    public PropertyAccessor(PropertyInfo propertyInfo)
    {
        _propertyInfo = propertyInfo;
        _name         = _propertyInfo.Name;
        _memberType   = _propertyInfo.PropertyType;

        _hasGetter = _propertyInfo.GetGetMethod(true) != null;
        if (_hasGetter)
        {
            _lateBoundGet = new Lazy <LateBoundGet>(() => DelegateFactory.CreateGet(_propertyInfo));
        }

        _hasSetter = propertyInfo.GetSetMethod(true) != null;
        if (_hasSetter)
        {
            _lateBoundSet = new Lazy <LateBoundSet>(() => DelegateFactory.CreateSet(_propertyInfo));
        }
    }
Пример #16
0
 public ReadOnlyPropertyAccessor(PropertyInfo readProperty) : base(readProperty.Name, readProperty.PropertyType)
 {
     _readProperty = readProperty;
     _getHandler   = DelegateFactory.CreateGet(readProperty);
 }
Пример #17
0
 protected override void Context()
 {
     base.Context();
     _objectUsedInSpecs.OneField = 33;
     _getHandler = DelegateFactory.CreateGet(ReflectionHelper.AllFieldsFor(typeof(SimpleObject)).First());
 }
Пример #18
0
 protected override void Context()
 {
     base.Context();
     _objectUsedInSpecs.OneProperty = "toto";
     _getHandler = DelegateFactory.CreateGet(ReflectionHelper.PropertyFor <SimpleObject, string>(x => x.OneProperty));
 }
Пример #19
0
        public void InitType(Type t)
        {
            logger.Debug("Initializing type: " + t.AssemblyQualifiedName);

            if (t.IsSimpleType())
            {
                return;
            }

            if (typeof(XContainer).IsAssignableFrom(t))
            {
                typesBeingInitialized.Add(t);

                return;
            }

            if (typeof(IEnumerable).IsAssignableFrom(t))
            {
                if (t.IsArray)
                {
                    typesToCreateForArrays[t] = typeof(List <>).MakeGenericType(t.GetElementType());
                }

                foreach (var g in t.GetGenericArguments())
                {
                    InitType(g);
                }

                //Handle dictionaries - initialize relevant KeyValuePair<T,K> types.
                foreach (var interfaceType in t.GetInterfaces())
                {
                    var arr = interfaceType.GetGenericArguments();
                    if (arr.Length != 1)
                    {
                        continue;
                    }

                    if (typeof(IEnumerable <>).MakeGenericType(arr[0]).IsAssignableFrom(t))
                    {
                        InitType(arr[0]);
                    }
                }

                if (t.IsGenericType && t.IsInterface) //handle IEnumerable<Something>
                {
                    var g = t.GetGenericArguments();
                    var e = typeof(IEnumerable <>).MakeGenericType(g);

                    if (e.IsAssignableFrom(t))
                    {
                        typesToCreateForEnumerables[t] = typeof(List <>).MakeGenericType(g);
                    }
                }

                if (t.IsGenericType && t.GetGenericArguments().Length == 1)
                {
                    var setType = typeof(ISet <>).MakeGenericType(t.GetGenericArguments());

                    if (setType.IsAssignableFrom(t)) //handle ISet<Something>
                    {
                        var g = t.GetGenericArguments();
                        var e = typeof(IEnumerable <>).MakeGenericType(g);

                        if (e.IsAssignableFrom(t))
                        {
                            typesToCreateForEnumerables[t] = typeof(List <>).MakeGenericType(g);
                        }
                    }
                }

                return;
            }

            var isKeyValuePair = false;

            var args = t.GetGenericArguments();

            if (args.Length == 2)
            {
                isKeyValuePair = (typeof(KeyValuePair <,>).MakeGenericType(args[0], args[1]) == t);
            }

            if (args.Length == 1 && args[0].IsValueType)
            {
                if (args[0].GetGenericArguments().Any() || typeof(Nullable <>).MakeGenericType(args[0]) == t)
                {
                    InitType(args[0]);

                    if (!args[0].GetGenericArguments().Any())
                    {
                        return;
                    }
                }
            }

            //already in the process of initializing this type (prevents infinite recursion).
            if (typesBeingInitialized.Contains(t))
            {
                return;
            }

            typesBeingInitialized.Add(t);

            var props = GetAllPropertiesForType(t, isKeyValuePair);

            typeToProperties[t] = props;
            var fields = GetAllFieldsForType(t);

            typeToFields[t] = fields;

            foreach (var p in props)
            {
                logger.Debug("Handling property: " + p.Name);

                DelegateFactory.CreateGet(p);
                if (!isKeyValuePair)
                {
                    DelegateFactory.CreateSet(p);
                }

                InitType(p.PropertyType);
            }

            foreach (var f in fields)
            {
                logger.Debug("Handling field: " + f.Name);

                DelegateFactory.CreateGet(f);
                if (!isKeyValuePair)
                {
                    DelegateFactory.CreateSet(f);
                }

                InitType(f.FieldType);
            }
        }