使用dynamic 类型动态调用方法

class Program
{
    static void Main(string[] args)
    {
        dynamic d = Activator.CreateInstance(typeof(DemoType));

        object o = d.ToString();

        Console.WriteLine(o);

        Console.WriteLine(d.Value);
    }
}


class DemoType
{
    public override string ToString()
    {
        return "Hello World";
    }


    public int Value
    {
        get
        {
            return 100;
        }
    }
}

你可能感兴趣的:(dynamic)