zoukankan      html  css  js  c++  java
  • 详解反射->Type.System

    反射先了解

    一:system.Type

    获取基本信息:

    Type.Name   //类名

    Type.FullName //完整路径

    Type.Namespace //空间名

            public class student
            {
                public int Id { set; get; }
                public string Name { set; get; }
                public int Age { set; get; }
            }
            static void Main(string[] args)
            {
                student s = new student() { Id = 1, Name = "zzj", Age = 12 };
                Type t = typeof(student);
                //查看类信息
                Console.WriteLine("Name={0},FullName={1},Namespace={2}", t.Name, t.FullName, t.Namespace);
             
            }
    View Code

    效果图:

    Name=student
    FullName=win32.反射+student
    Namespace=win32

    二。反射下的几个重要方法

    System.Reflection.

    PropertyInfo:封装类型属性信息

    ConstructorInfo:类型构造函数

    MethodInfo:类型的方面名称

    EventInfo:类型事件信息

    ParameterInfo:方法、构造函数信息

    MemberInfo:成员类型 Type.GetMembers()/GetMember()/FindMember()

    1.成员信息 与 memberInfo

                StringBuilder sb = new StringBuilder();
                MemberInfo[] me = t.GetMembers();//get page member
                sb.Append("查看类型" + t.Name + t.Name + "的成员信息");
                foreach (MemberInfo m in me) {
                    sb.Append("成员"+m.ToString().PadRight(50)+"类型:"+m.MemberType+"
    ");
                }
                Console.WriteLine(sb);
    View Code
    查看类型studentstudent的成员信息成员Void set_Id(Int32)
          类型:Method
    成员Int32 get_Id()                                    类型:Method
    成员Void set_Name(System.String)                      类型:Method
    成员System.String get_Name()                          类型:Method
    成员Void set_Age(Int32)                               类型:Method
    成员Int32 get_Age()                                   类型:Method
    成员System.String ToString()                          类型:Method
    成员Boolean Equals(System.Object)                     类型:Method
    成员Int32 GetHashCode()                               类型:Method
    成员System.Type GetType()                             类型:Method
    成员Void .ctor()                                      类型:Constructor
    成员Int32 Id                                          类型:Property
    成员System.String Name                                类型:Property
    成员Int32 Age                                         类型:Property
    成员win32.反射+student+major                            类型:NestedType
    
    请按任意键继续. . .
    View Code

    2.字段信息与FieldInfo类型

    如果希望获取一个类型的所有字段,可以使用GetFields()方法,再次添加一个方法
    FieldExplore():(但是感觉只能获取属性,还有静态)

            static void FieldExplor(Type t) {
                StringBuilder sb = new StringBuilder();
                FieldInfo[] fields = t.GetFields();
                sb.Append("查看类型" + t.Name + "的字段信息:
    ");
                sb.Append(string.Empty.PadLeft(50, '-') + "
    ");
                foreach (FieldInfo fi in fields) {
                    sb.Append("名称:" + fi.Name + "
    ");
                    sb.Append("类型:" + fi.FieldType + "
    ");
                    sb.Append("特性:" + fi.Attributes + "
    ");
                }
                Console.WriteLine(sb.ToString());
    
            }
    View Code

    查看类型student的字段信息:
    --------------------------------------------------
    名称:love
    类型:System.String
    特性:Public
    名称:Lv
    类型:System.Int32
    特性:Public, Static

    3.属性信息与PropertyInfo类型

            static void PropertydExplor(Type t)
            {
                StringBuilder sb = new StringBuilder();
                PropertyInfo[] fields = t.GetProperties();
                sb.Append("查看类型" + t.Name + "的属性信息:
    ");
                sb.Append(string.Empty.PadLeft(50, '-') + "
    ");
                foreach (PropertyInfo fi in fields)
                {
                    sb.Append("名称:" + fi.Name + "
    ");
                    sb.Append("类型:" + fi.PropertyType + "
    ");
                    sb.Append("可读:" + fi.CanRead + "
    ");
                    sb.Append("可写:" + fi.CanWrite + "
    ");
                    sb.Append("特性:" + fi.Attributes + "
    ");
                }
                Console.WriteLine(sb.ToString());
    
            }
    View Code

    查看类型student的属性信息:
    --------------------------------------------------
    名称:Id
    类型:System.Int32
    可读:True
    可写:True
    特性:None
    名称:Name
    类型:System.String
    可读:True
    可写:True
    特性:None
    名称:Age
    类型:System.Int32
    可读:True
    可写:True
    特性:None

    4.方法信息与MethodInfo类型

            static void MethodExplore(Type t)
            {
                StringBuilder sb = new StringBuilder();
                MethodInfo[] me = t.GetMethods();//get page member
                sb.Append("查看类型" + t.Name + t.Name + "的成员信息");
                foreach (MethodInfo m in me)
                {
                    sb.Append("名称:" + m.Name + "
    ");
                    sb.Append("签名:" + m.ToString() + "
    ");
                    sb.Append("属性:" + m.Attributes + "
    ");
                    sb.Append("返回值类型:" + m.ReturnType + "
    ");
                }
                Console.WriteLine(sb);
            }
    View Code

    查看类型studentstudent的成员信息名称:set_Id
    签名:Void set_Id(Int32)
    属性:PrivateScope, Public, HideBySig, SpecialName
    返回值类型:System.Void
    名称:get_Id
    签名:Int32 get_Id()
    属性:PrivateScope, Public, HideBySig, SpecialName
    返回值类型:System.Int32
    名称:set_Name
    签名:Void set_Name(System.String)
    属性:PrivateScope, Public, HideBySig, SpecialName
    返回值类型:System.Void
    名称:get_Name
    签名:System.String get_Name()
    属性:PrivateScope, Public, HideBySig, SpecialName
    返回值类型:System.String
    名称:set_Age
    签名:Void set_Age(Int32)
    属性:PrivateScope, Public, HideBySig, SpecialName
    返回值类型:System.Void
    名称:get_Age
    签名:Int32 get_Age()
    属性:PrivateScope, Public, HideBySig, SpecialName
    返回值类型:System.Int32
    名称:ToString
    签名:System.String ToString()
    属性:PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask
    返回值类型:System.String
    名称:Equals
    签名:Boolean Equals(System.Object)
    属性:PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask
    返回值类型:System.Boolean
    名称:GetHashCode
    签名:Int32 GetHashCode()
    属性:PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask
    返回值类型:System.Int32
    名称:GetType
    签名:System.Type GetType()
    属性:PrivateScope, Public, HideBySig
    返回值类型:System.Type

    5.ConstructorInfo类型和EventInfo类型

  • 相关阅读:
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列(important!)
    从上往下打印二叉树
    最小的k个数(important!)
    扑克牌顺子
    栈的压入、弹出序列(important!)
    和为s的连续正数序列(important!)
    数组中只出现一次的数字
    fgets()函数以及fputs()函数
    C语言中的指针
  • 原文地址:https://www.cnblogs.com/0to9/p/5103028.html
Copyright © 2011-2022 走看看