阅读背景:

Typeof()与 GetType() ,获取对象的所有公有属性和所有公有方法GetProperties()GetMethods()

来源:互联网 

<1>

TypeOf()与GetType()的区别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace @typeof
{
    // TypeOf() 和 GetType()的区别:  
    //<1> TypeOf():得到一个Class的Type
    //<2> GetType():得到一个Class的实例的Type



    //Typeof()是运算符而GetType是方法
    //GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用(初始化以后)
    //Typeof()的参数只能是int,string,String,自定义类型,且不能是实例
    class Program
    {        
        static void Main(string[] args)
        {
           //得到一个Class的Type
           var type = typeof(int);
           Console.WriteLine(type);  //输出System.int32




           //得到一个Class的Type
           var type3 = typeof(Program);
           Console.WriteLine(type3); //输出typeof.Program
           
           
            //-----------------------------------------------------------

           

           Program p = new Program();

           //得到一个Class的实例的Type
           var type2= p.GetType();
           Console.WriteLine(type2); //输出typeof.Program

           Console.ReadKey();
        }
    }
}
using System;



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: