标签:style blog http color 使用 os strong io
1 dynamic d = GetSomeObject(); 2 d.Quack();
1 public class Test 2 { 3 public dynamic Foo; 4 } 5 等同于 6 public class Test 7 { 8 [System.Runtime.CompilerServices.DynamicAttribute] 9 public object Foo; 10 }
int i = 7; dynamic d = i; long j = d; // No cast required (implicit conversion) int i = 7; dynamic d = i; short j = d; // throws RuntimeBinderException
class Program { static void Foo (object x, object y) { Console.WriteLine ("oo"); } static void Foo (object x, string y) { Console.WriteLine ("os"); } static void Foo (string x, object y) { Console.WriteLine ("so"); } static void Foo (string x, string y) { Console.WriteLine ("ss"); } static void Main() { object o = "hello"; dynamic d = "goodbye"; Foo (o, d); // os } }
class ToXElementPersonVisitor { public XElement DynamicVisit (Person p) { return Visit ((dynamic)p); } XElement Visit (Person p) { return new XElement ("Person", new XAttribute ("Type", p.GetType().Name), new XElement ("FirstName", p.FirstName), new XElement ("LastName", p.LastName), p.Friends.Select (f => DynamicVisit (f)) ); } XElement Visit (Customer c) // Specialized logic for customers { XElement xe = Visit ((Person)c); // Call "base" method xe.Add (new XElement ("CreditLimit", c.CreditLimit)); return xe; } XElement Visit (Employee e) // Specialized logic for employees { XElement xe = Visit ((Person)e); // Call "base" method xe.Add (new XElement ("Salary", e.Salary)); return xe; } }
class ToXElementPersonVisitor : PersonVisitor<XElement> { protected override XElement Visit(Person p) { return new XElement("Person", new XAttribute("Type", p.GetType().Name), new XElement("FirstName", p.FirstName), new XElement("LastName", p.LastName), p.Friends.Select(f => DynamicVisit(f)) ); } protected override XElement Visit(Customer c) { XElement xe = base.Visit(c); xe.Add(new XElement("CreditLimit", c.CreditLimit)); return xe; } protected override XElement Visit(Employee e) { XElement xe = base.Visit(e); xe.Add(new XElement("Salary", e.Salary)); return xe; } }
标签:style blog http color 使用 os strong io
原文地址:http://www.cnblogs.com/robyn/p/3863443.html