欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何利用反射构建元数据查看器

程序员文章站 2023-12-19 13:23:40
原理比较简单,引入system.reflection命名空间,利用反射查看某种type下的方法,属性,字段和支持的接口等。复制代码 代码如下:using system;us...

原理比较简单,引入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: ********


上一篇:

下一篇: