c#动态类型Dynamic

需引用System.Dynamic命名空间

来源:http://www.cnblogs.com/ryanding/archive/2010/12/09/1900106.html

dynamic Customer = new ExpandoObject();
Customer.Name = "Lucy";
Customer.Age = 20;
Customer.Female = true;
Console.WriteLine(Customer.Name + Customer.Age + Customer.Female);
Console.ReadKey();
//
static class Calculator {
    public static T Add(T t1, T t2) {
        dynamic d1 = t1;
        dynamic d2 = t2;

        return (T)(d1 + d2);
    }
}

public static void Main(string[] args){
    int i = Calculator.Add(1, 2);
    double d = Calculator.Add(1.1, 2.2);
    string s = Calculator.Add("abc", "def");
    Console.WriteLine(i + " " + d + " " + s);
    //3  3.3  abcdef
}

转载于:https://www.cnblogs.com/Jerrycjc/p/5166590.html

你可能感兴趣的:(c#动态类型Dynamic)