コード例 #1
0
        public void AddTest()
        {
            dynamic obj = new ElasticEntity();

            (obj as IDictionary <string, object>).Add("Nome", "vitor");
            obj.Oid = 1;
            obj.Id  = 2;

            Assert.AreEqual("vitor", obj.Nome);
            Assert.AreEqual(1, obj.Oid);
            Assert.AreEqual(2, obj.Id);

            Assert.IsTrue((obj as IDictionary <string, object>).ContainsKey("Nome"));
            Assert.IsTrue((obj as IDictionary <string, object>).ContainsKey("Oid"));
            Assert.IsTrue((obj as IDictionary <string, object>).ContainsKey("Id"));

            var tipo = obj.GetType();

            Assert.AreEqual(typeof(ElasticEntity), tipo);

            var nomes = (obj.GetDynamicMemberNames() as IEnumerable <string>);

            Assert.AreEqual(3, nomes.Count());
            Assert.AreEqual(3, (obj.AsEnumerable() as IEnumerable <KeyValuePair <string, object> >).Count());
            Assert.AreEqual(3, (obj as IDictionary <string, object>).Count);
            Assert.AreEqual(3, (obj as ICollection <KeyValuePair <string, object> >).Count);
        }
コード例 #2
0
        public void ReflectionTest()
        {
            dynamic obj = new ElasticEntity();

            obj.Id    = 1000;
            obj.Oid   = 2;
            obj.Nome  = "vitor";
            obj.Data  = DateTime.Today;
            obj.Valor = 1.23;
            (obj as IDictionary <string, object>).Add("Foo", "teste foo");
            (obj as IDictionary <string, object>)["Bar"] = "teste bar";

            foreach (var x in obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
            {
                if (x.Name == "Item")
                {
                    continue;
                }
                Console.WriteLine($"{x.Name}:{x.GetValue(obj)}");
            }
        }