zoukankan      html  css  js  c++  java
  • typeof和GetType()获取Type类型

    typeof

    typeof是运算符,获取某一类型的 System.Type 对象。 typeof()的参数只能是int,string,String,自定义类型,且不能是实例。 Type t = typeof(int);

    GetType()

    方法,获取当前实例的类型 int i = 10; Console.WriteLine(i.GetType());

    typeof和GetType的联系和区别

    Typeof()是运算符而GetType是方法 GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用(初始化以后) Typeof()的参数只能是int,string,String,自定义类型,且不能是实例 具体代码示例:
        class Program
        {
            public int TestMember;
            public void SampleMethod() {}
            public int TestInt()
            {
                return 0;
            }
            static void Main(string[] args)
            {
                Type t = typeof (Program);
                Console.WriteLine("属性:");
                Console.WriteLine(t.Name);
                Console.WriteLine(t.FullName);
                Console.WriteLine(t.Assembly);
                Console.WriteLine(t.IsAbstract);
                Console.WriteLine(t.IsEnum);
                Console.WriteLine(t.IsClass);
                Console.WriteLine("方法:");
                MethodInfo[] methodInfos = t.GetMethods();
                foreach (MethodInfo mInfo in methodInfos)
                    Console.WriteLine(mInfo.ToString());
    
                Console.WriteLine("成员:");
                MemberInfo[] memberInfo = t.GetMembers();
                foreach (MemberInfo mInfo in memberInfo)
                    Console.WriteLine(mInfo.ToString());
                Console.ReadKey();
            }
        }
    
    
    运行结果如下图: type
  • 相关阅读:
    5773. 【NOIP2008模拟】简单数学题
    jzoj_5455. 【NOIP2017提高A组冲刺11.6】拆网线
    5461. 【NOIP2017提高A组冲刺11.8】购物
    博客第三天
    博客第二天
    博客的第一天。
    微服务架构下的session一致性
    分布式数据库数据一致性的原理、与技术实现方案
    epoll的本质
    DNS域名解析过程
  • 原文地址:https://www.cnblogs.com/vsdot/p/3263330.html
Copyright © 2011-2022 走看看