Ejemplo n.º 1
0
        private static string ExtractString(dynamic field)
        {
            if (field == null)
            {
                return("");
            }

            if (field is string)
            {
                return(field.ToString());
            }

            var result = "";

            if (field is Dictionary <string, string> )
            {
                foreach (string prop in field.Keys)
                {
                    result += " " + ExtractString(field[prop]);
                }

                return(result);
            }

            var properties = PropertyReflector.GetProperties(field);

            foreach (string prop in properties.Keys)
            {
                result += " " + ExtractString(properties[prop]);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public void TestGetNestedProperties()
        {
            TestClass obj = new TestClass();

            obj.NestedProperty = new TestNestedClass()
            {
                IntProperty = 10
            };

            Dictionary <string, object> map = PropertyReflector.GetProperties(obj);

            Assert.Equal(3, map.Count);
            Assert.True(map["NestedProperty"] is TestNestedClass);
            Assert.Equal(10, (map["NestedProperty"] as TestNestedClass).IntProperty);
        }
Ejemplo n.º 3
0
        public void TestGetProperties()
        {
            TestClass     obj   = new TestClass();
            List <string> names = PropertyReflector.GetPropertyNames(obj);

            Assert.Equal(3, names.Count);
            Assert.Contains("PublicField", names);
            Assert.Contains("PublicProp", names);

            Dictionary <string, object> map = PropertyReflector.GetProperties(obj);

            Assert.Equal(3, map.Count);
            Assert.Equal("ABC", map["PublicField"]);
            Assert.NotNull(map["PublicProp"]);
        }
Ejemplo n.º 4
0
        private static string ExtractString(dynamic field)
        {
            var result = "";

            switch (field)
            {
            case null:
                return(result);

            case string _:
                return(field.ToString());

            case IDictionary _:
            {
                foreach (var prop in field.Keys)
                {
                    result += " " + ExtractString(field[prop]);
                }
                return(result);
            }

            case IEnumerable _:
            {
                foreach (var prop in field)
                {
                    result += " " + ExtractString(prop);
                }
                return(result);
            }
            }

            var properties = PropertyReflector.GetProperties(field);

            foreach (string prop in properties.Keys)
            {
                result += " " + ExtractString(properties[prop]);
            }
            return(result);
        }