国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

如何利用反射構建元數據查看器

2020-01-24 03:14:26
字體:
來源:轉載
供稿:網友

原理比較簡單,引入System.Reflection命名空間,利用反射查看某種Type下的方法,屬性,字段和支持的接口等。

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using System.Xml;
using System.Data;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Please input a type:");
                string typeStr = Console.ReadLine();

                if (typeStr == "exit" || typeStr == "quit")
                    break;

                try
                {
                    Type type = Type.GetType(typeStr);
                    ListFields(type);
                    ListMethods(type);
                    ListInterfaces(type);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("It is not a valid type!");
                }
            }

        }

        #region Methods
        public static void ListFields(Type type)
        {
            Console.WriteLine("******** Fields: ********");
            //foreach (FieldInfo item in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Default))
            foreach (FieldInfo item in type.GetFields())
            {
                Console.WriteLine("->" + item.Name);
            }
            Console.WriteLine("");
        }

        public static void ListMethods(Type type)
        {
            Console.WriteLine("******** Methods: ********");
            //foreach (var item in type.GetMethods(BindingFlags.Default | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic))
            var methodInfo = type.GetMethods().Select(m => m.Name).Distinct();
            foreach (var item in methodInfo)
            {
                Console.WriteLine("->" + item);
            }
            Console.WriteLine("");
        }

        public static void ListInterfaces(Type type)
        {
            Console.WriteLine("******** Interfaces: ********");
            foreach (var item in type.GetInterfaces())
            {
                Console.WriteLine("->" + item.Name);
            }
            Console.WriteLine("");
        }

        public static void ListProperties(Type type)
        {
            Console.WriteLine("******** Properties: ********");
            foreach (var item in type.GetProperties())
            {
                Console.WriteLine("->" + item.Name);
            }
            Console.WriteLine("");
        }
        #endregion

    }

}


測試case 1:
復制代碼 代碼如下:

Please input a type:
System.Int32
******** Fields: ********
->MaxValue
->MinValue

******** Methods: ********
->CompareTo
->Equals
->GetHashCode
->ToString
->Parse
->TryParse
->GetTypeCode
->GetType

******** Interfaces: ********
->IComparable
->IFormattable
->IConvertible
->IComparable`1
->IEquatable`1


測試case 2:
復制代碼 代碼如下:

Please input a type:
System.Math
******** Fields: ********
->PI
->E

******** Methods: ********
->Acos
->Asin
->Atan
->Atan2
->Ceiling
->Cos
->Cosh
->Floor
->Sin
->Tan
->Sinh
->Tanh
->Round
->Truncate
->Sqrt
->Log
->Log10
->Exp
->Pow
->IEEERemainder
->Abs
->Max
->Min
->Sign
->BigMul
->DivRem
->ToString
->Equals
->GetHashCode
->GetType

******** Interfaces: ********


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宕昌县| 大方县| 文水县| 建湖县| 平顶山市| 洪洞县| 县级市| 星子县| 德兴市| 双鸭山市| 临泽县| 延边| 儋州市| 聊城市| 辽宁省| 蒲城县| 滕州市| 山阴县| 周口市| 榆林市| 集贤县| 南丰县| 庆城县| 皮山县| 东兰县| 阜平县| 海晏县| 金昌市| 崇左市| 五大连池市| 微博| 永年县| 兰州市| 北宁市| 宾川县| 乌拉特中旗| 海宁市| 法库县| 陇西县| 安义县| 宿州市|