Exemple #1
0
        static void Main1234(string[] args)
        {
            //object o = GetASpeaker();//assume that we dont know the kind of obejct we are getitng from the setSpeak() method
            ////here we can use the refelection
            //o.GetType().GetMethod("Speak").Invoke(o,null);

            //without reflection we can access the speak method using dyunamic keyword
            dynamic o = GetASpeaker();

            o.Speak();                             //it will be resolvod at run time..
            dynamic expando = new ExpandoObject(); //type dynamically

            expando.Name  = "Vishal Singh";
            expando.Speak = new Action(() => Console.WriteLine(expando.Name));
            foreach (var member in expando)
            {
                //Console.WriteLine(member.key);
            }

            //var doc = XDocument.Load("EmployeeXml.xml");
            //foreach(var element in doc.Element("Employees").Elements("Employee"))
            //{
            //    Console.WriteLine(element.Element("FirstName").Value);
            //}


            //using dynamic obejct
            dynamic doc = new DynamicXml("EmployeeXml.xml");

            foreach (var employee in doc.Employees)
            {
                Console.WriteLine(employee.FirstName);
            }
        }
Exemple #2
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)//it will be invoked when wee try to invoke .firstName or .method() name
        {
            var xml = _xml.Element(binder.Name);

            if (xml != null)
            {
                result = new DynamicXml(xml);
                return(true);
            }
            result = null;
            return(false);
        }