[反射]通用方法 命名空间,类,对象,属性

反射后的类型基础通用类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;

namespace Reflection_Test
{
    public class DLLAnalyst
    {
        private string fileName;

        public string FileName
        {
            get { return fileName; }
            set
            { 
                fileName = value;
                this.myAssembly = Assembly.LoadFile(this.fileName);
            }
        }

        private Assembly myAssembly = null;

        public DLLAnalyst() { }

        public DLLAnalyst(string fileName)
        {
            this.fileName = fileName;

            this.myAssembly = Assembly.LoadFile(this.fileName);
        }

        public List<string> GetNameSpaces()
        {
            if (this.myAssembly == null) return null;

            List<string> list = new List<string>();

            Type[] types = this.myAssembly.GetTypes();
            foreach (Type type in types)
            {
                if (!list.Contains(type.Namespace))
                {
                    list.Add(type.Namespace);
                }
            }

            

            return list;
        }

        public List<ClassInfo> GetClasses()
        {
            if (string.IsNullOrEmpty(this.fileName)) return null;

            List<ClassInfo> list = new List<ClassInfo>();

            

            Type[] types = this.myAssembly.GetTypes();

            foreach (Type type in types)
            {
                ClassInfo classInfo = new ClassInfo();
                classInfo.NameSpace = type.Namespace;
                classInfo.Name = type.Name;
                classInfo.FullName = type.FullName;
                classInfo.Type = type;

                list.Add(classInfo);
            }

            return list;
        }
        
        //public List<MethodInfo> GetMethodByClass(ClassInfo classInfo)
        //{
        //    if (this.myAssembly == null) return null;

        //    MethodInfo[] methods = classInfo.Type.GetMethods();

        //    List<MethodInfo> list = new List<MethodInfo>();

        //    list.AddRange(methods);

        //    return list;
        //}


    }


    public class ClassInfo
    {
        private string nameSpace;

        public string NameSpace
        {
            get { return nameSpace; }
            set { nameSpace = value; }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string fullName;

        public string FullName
        {
            get { return fullName; }
            set { fullName = value; }
        }

        private Type type;

        public Type Type
        {
            get { return type; }
            set { type = value; }
        }

        

        public ClassInfo() { }

        public ClassInfo(string nameSpace, string name,string fullName,Type type)
        {
            this.nameSpace = nameSpace;
            this.name = name;
            this.fullName = fullName;
            this.type = type;
        }
        
    }
}

反射类映射显示

private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "动态库文件(*.dll)|*.dll|可执行文件(*.exe)|*.exe";

            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                string fileName = openDlg.FileName;

                DLLAnalyst analyst = new DLLAnalyst(fileName);
                List<string> nameSpaces= analyst.GetNameSpaces();

                this.tvClass.Nodes.Clear();

                List<ClassInfo> classes = analyst.GetClasses();

                foreach (string nameSpace in nameSpaces)
                {            
                    TreeNode treeNode = this.tvClass.Nodes.Add(nameSpace);

                    foreach (ClassInfo classInfo in classes)
                    {
                        if (classInfo.NameSpace == nameSpace)
                        {
                            TreeNode nodeClass = treeNode.Nodes.Add(classInfo.Name);
                            nodeClass.Tag = classInfo;
                        }
                    }
                    
                }
            }
        }
原文地址:https://www.cnblogs.com/mjxxsc/p/7457477.html