C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型。

typeof(x)中的x,必须是具体的类名、类型名称等,不可以是变量名称;GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用。

  1. Typeof的参数只能是int,string,String,自定义类型,且不能是实例
  2. GetType()和typeof都返回System.Type的引用.
  3. TypeOf():得到一个Class的Type
  4. GetType():得到一个Class的实例的Type

例子:

 string s = testone.GetType().ToString();

 Type type = typeof(Int32);

相关文章:

  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2022-01-20
  • 2022-12-23
猜你喜欢
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2021-07-06
  • 2021-08-25
相关资源
相似解决方案