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();
        }
    }
  • 相关阅读:
    Tornado web 框架
    mysql_orm模块操作数据库(17.6.29)
    mysql小结篇3 索引、分页、执行计划--(17.6.28)
    Oracle触发器Trigger2行级
    Oracle触发器Trigger基础1
    Oracle函数function
    Oracle异常的抛出处理
    Oracle利用过程procedure块实现银行转账
    Oracle存储过程procedure
    PL/SQL块loop..各种循环练习
  • 原文地址:https://www.cnblogs.com/nimorl/p/12626194.html
Copyright © 2011-2022 走看看