Exemplo n.º 1
0
        public void TestGetProperty()
        {
            TestClass obj = new TestClass();

            object value = PropertyReflector.GetProperty(obj, "privateField");

            Assert.Null(value);

            value = PropertyReflector.GetProperty(obj, "publicField");
            Assert.Equal("ABC", value);

            value = PropertyReflector.GetProperty(obj, "PublicProp");
            Assert.NotNull(value);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Extracts hash tags from selected fields in an object.
        /// </summary>
        /// <param name="obj">an object which contains hash tags.</param>
        /// <param name="searchFields">a list of fields in the objects where to extract tags</param>
        /// <returns>a list of extracted and compressed tags.</returns>
        public static string[] ExtractHashTagsFromValue(dynamic obj, params string[] searchFields)
        {
            // Todo: Use recursive
            string[] tags = CompressTags(obj.Tags.ToArray());

            foreach (string field in searchFields)
            {
                string text = ExtractString(PropertyReflector.GetProperty(obj, field));

                if (text != "")
                {
                    string[] hashTags = (from Match match in Regex.Matches(text, HASHTAG_REGEX) select match.Value).ToArray();
                    tags = tags.Concat(CompressTags(hashTags).ToList()).ToArray();
                }
            }

            return(tags.Distinct().ToArray());
        }
Exemplo n.º 3
0
        //protected override string ResolvePropertyName(string propertyName)
        //{
        //	return base.ResolvePropertyName(propertyName);
        //}

        //protected override JsonProperty CreatePropertyFromConstructorParameter(JsonProperty matchingMemberProperty, ParameterInfo parameterInfo)
        //{
        //	return base.CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
        //}

        //protected override IValueProvider CreateMemberValueProvider(MemberInfo member)
        //{
        //	return base.CreateMemberValueProvider(member);
        //}

        /// <summary>
        ///
        /// </summary>
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

            if (member.DeclaringType == typeof(Exception))
            {
                string valueName;
                if (ExceptionFieldValueToNameDictionary.TryGetValue(member.Name, out valueName))
                {
                    //property.ValueProvider = new ValueProvider(member.Name, valueName, member);
                    var valueProvider = new ValueProvider(PropertyReflector.MakeObjectPropertyGetter(PropertyReflector.GetProperty(typeof(Exception), member.Name)),
                                                          PropertyReflector.MakeObjectFieldSetter(PropertyReflector.GetField(typeof(Exception), valueName)))
                    {
                        Tag = string.Format("DeclaringType:{0}, Read: {1}, Write: {2}", member.DeclaringType.FullName, member.Name, valueName)
                    };
                    property.ValueProvider = valueProvider;
                    property.Writable      = true;
                }
            }

            return(property);
        }
Exemplo n.º 4
0
        internal static void PropertyReflectTest()
        {
            {
                var fGetter = PropertyReflector.MakeObjectFieldGetter(PropertyReflector.GetField(typeof(MyClass), "Name"));
                var fSetter = PropertyReflector.MakeObjectFieldSetter(PropertyReflector.GetField(typeof(MyClass), "Name"));

                var pGetter = PropertyReflector.MakeObjectPropertyGetter(PropertyReflector.GetProperty(typeof(MyClass), "Weight"));
                var pSetter = PropertyReflector.MakeObjectPropertySetter(PropertyReflector.GetProperty(typeof(MyClass), "Weight"));

                try
                {
                    var mc1 = new MyClass();

                    var name = "Chris";
                    fSetter(mc1, name);
                    var rname = fGetter(mc1);
                    if (name != (string)rname)
                    {
                        throw new Exception();
                    }

                    var weight = 62;
                    pSetter(mc1, weight);
                    var rweight = (int)pGetter(mc1);
                    if (weight != rweight)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("****** PropertyReflectTest() test failed");
                    throw;
                }
            }

            {
                var fGetter = PropertyReflector <MyClass, string> .GetFieldGetterFunc("Name");

                var fSetter = PropertyReflector <MyClass, string> .GetFieldSetterFunc("Name");

                var pGetter = PropertyReflector <MyClass, int> .GetPropertyGetterFunc("Weight");

                var pSetter = PropertyReflector <MyClass, int> .GetPropertySetterFunc("Weight");

                try
                {
                    var mc1 = new MyClass();

                    var name = "Chris";
                    fSetter(mc1, name);
                    var rname = fGetter(mc1);
                    if (name != rname)
                    {
                        throw new Exception();
                    }

                    var weight = 62;
                    pSetter(mc1, weight);
                    var rweight = pGetter(mc1);
                    if (weight != rweight)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("****** PropertyReflectTest() generic test failed");
                    throw;
                }
            }
        }