page contents

c#中GetType()与Typeof()的区别

本文讲述了c#中GetType()与Typeof()的区别!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-06-XtLRRGVw62a9544ba686c.png

本文讲述了c#中GetType()与Typeof()的区别!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

案例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

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-06-15 11:39
  • 阅读 ( 277 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章