博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Type
阅读量:7107 次
发布时间:2019-06-28

本文共 3069 字,大约阅读时间需要 10 分钟。

C#中通过Type类可以访问任意数据类型信息。

1.获取给定类型的Type引用有3种方式:
  a.使用typeof运算符,如Type t = typeof(int);
  b.使用GetType()方法,如int i;Type t = i.GetType();
  c.使用Type类的静态方法GetType(),如Type t =Type.GetType("System.Double");
2.Type的属性:
  Name:数据类型名;
  FullName:数据类型的完全限定名,包括命名空间;
  Namespace:数据类型的命名空间;
  BaseType:直接基本类型;
  UnderlyingSystemType:映射类型;
3.Type的方法:
  GetMethod():返回一个方法的信息;
  GetMethods():返回所有方法的信息。
TestType.cs:

using System; using System.Reflection; namespace Magci.Test.Reflection {
    public class TestType     {
        public static void Main()         {
            //基本数据类型             Type intType = typeof(int);                          //属性             Console.WriteLine("intType.Name = " + intType.Name);             Console.WriteLine("intType.FullName = " + intType.FullName);             Console.WriteLine("intType.Namespace = " + intType.Namespace);             Console.WriteLine("intType.IsAbstract = " + intType.IsAbstract);             Console.WriteLine("intType.IsClass = " + intType.IsClass);             Console.WriteLine("intType.IsEnum = " + intType.IsEnum);             Console.WriteLine("intType.IsPrimitive = " + intType.IsPrimitive);             Console.WriteLine("intType.IsValueType = " + intType.IsValueType);             //方法             MethodInfo[] methods = intType.GetMethods();             foreach (MethodInfo method in methods)             {
                Console.WriteLine(method.DeclaringType + " " + method.MemberType + " " + method.Name);             }         }     } }

TestTypeView.cs:

using System; using System.Text; using System.Windows.Forms; using System.Reflection; namespace Magci.Test.Reflection {
    public class TestTypeView     {
        public static StringBuilder OutputText = new StringBuilder();         public static void Main()         {
            Type t = typeof(double);             AnalyzeType(t);             MessageBox.Show(OutputText.ToString());         }         public static void AnalyzeType(Type t)         {
            //数据类型名             OutputText.Append("\nType Name: " + t.Name);             //数据类型的完全限定名,包括命名空间             OutputText.Append("\nFull Name: " + t.FullName);             //数据类型的命名空间             OutputText.Append("\nNamespace: " + t.Namespace);                          //直接基本类型             Type tBase = t.BaseType;             if (tBase != null)             {
                OutputText.Append("\nBase Type: " + tBase.Name);             }                          //映射类型             Type tUnderlyingSystem = t.UnderlyingSystemType;             if (tUnderlyingSystem != null)             {
                OutputText.Append("\nUnderlyingSystem Type: " + tUnderlyingSystem.Name);             }             //所有公共方法             OutputText.Append("\n\nPublic members:");             MemberInfo[] methods = t.GetMethods();             foreach (MemberInfo method in methods)             {
                OutputText.Append("\n" + method.DeclaringType + " " + method.MemberType + "" + method.Name);             }         }     } }

转载于:https://www.cnblogs.com/kingdom_0/articles/2040855.html

你可能感兴趣的文章
新浪微博中的特殊符号解释
查看>>
PropertyGrid无意的发现DisplayNameAttribute及应用
查看>>
linux查看端口所占用的进程号
查看>>
BI开发之——多维立方体(Cube)
查看>>
[转]SQLServerDBA十大必备工具---让生活轻松点
查看>>
程序集之GAC---Global Assembly Cache
查看>>
分享一个帮助你自定义标签并且兼容现代浏览器的javascript类库 : X-tag
查看>>
一道恶心题的流氓解法(HUD 4002 Find the maximum)
查看>>
博客管理杂记-7月29日
查看>>
WPF中的DataTemplate绑定使用的场合
查看>>
最值栈
查看>>
boost::timer
查看>>
利用SqlBulkCopy插入数据
查看>>
spring + ehcache 实例
查看>>
JS模板和JSON数据的结合
查看>>
关于面试宝典中的-检测并修改不适合的继承
查看>>
有关T-SQL的10个好习惯 转载http://www.cnblogs.com/CareySon/archive/2012/10/11/2719598.html
查看>>
Qt的QWaitCondition
查看>>
有钱不赚的老板
查看>>
PHP eval() 函数
查看>>