zoukankan      html  css  js  c++  java
  • GetType()与Typeof()的区别 举了2个案例

    GetType()与Typeof()区别

    GetType()返回的是对象的类名
    案例1:

    int i = 5;
    Console.WriteLine(i.GetType());//System.Int32
    var x = 127.25m;
    Console.WriteLine(x.GetType());//System.Decimal

    案例2:

    namespace _2011._12._15
    {
    class Program
    {
    static void Main(string[] args)
    {
    Test testone = new Test();
    string s = testone.GetType().ToString();
    Console.WriteLine(s);//_2011._12._15.Test 命名空间的Test类
    }
    }
    class Test
    {
    }
    }



    Typeof()返回的是类名的对象,也可以返回类名,也可以返回特定类内部的方法和字段

    namespace _2011._12._15
    {
    class Program
    {
    static void Main(string[] args)
    {
    Test testone = new Test();
    string s = testone.GetType().ToString();
    Console.WriteLine("GetType():");
    Console.WriteLine(s);//_2011._12._15.Test 命名空间的Test类

    Type type = typeof(Test);
    Console.WriteLine("Typeof():");
    Console.WriteLine(type);//_2011._12._15.Test 命名空间的Test类
    Console.WriteLine();

    MethodInfo[] methodinfo = type.GetMethods();

    Console.WriteLine(methodinfo.GetType());//System.Reflection.MethodInfo[]
    foreach (var i in methodinfo)
    {
    Console.WriteLine(i);//输出Test类的所有方法及继承Object的实例方法
    }
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine();
    MemberInfo[] memberinfo = type.GetMembers();
    Console.WriteLine(memberinfo.GetType());
    foreach(var i in memberinfo)
    {
    Console.WriteLine(i);//输出Test类字段和System.type类型
    }
    }


    }

    class Test
    {

    private int age;
    public string name;
    public void speaking()
    {
    Console.WriteLine("Welcome to cnblog!");
    }


    public void writing()
    {
    Console.WriteLine("Please writing something!");
    }
    }
    }

    运行结果:

    GetType():
    _2011._12._15.Test
    Typeof():
    _2011._12._15.Test

    System.Reflection.MethodInfo[]
    Void speaking()
    Void writing()
    System.Type GetType()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()




    System.Reflection.MemberInfo[]
    Void speaking()
    Void writing()
    System.Type GetType()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()
    Void .ctor()
    System.String name




       
       

  • 相关阅读:
    Android中Alarm的机制
    String字符串操作--切割,截取,替换,查找,比较,去空格.....
    时间类(时间戳的各种转换成)
    android 常用时间格式转换代码
    Android时间戳与字符串相互转换
    Android时间对话框TimePickerDialog介绍
    一种基于Qt的可伸缩的全异步C/S架构server实现(一) 综述
    C++ overloading contructor
    特征生成
    Atitit.软件仪表盘(2)--vm子系统--资源占用监測
  • 原文地址:https://www.cnblogs.com/IAmBetter/p/2288990.html
Copyright © 2011-2022 走看看