首页 > Windows开发 > 详细

c# aseembly 反射.dll文件获取源代码的结构

时间:2017-08-02 18:48:38      阅读:329      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsProgramTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void openAssembleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var openFile = new OpenFileDialog();
            if (openFile.ShowDialog(this) == DialogResult.OK)
            {
                var assembly = Assembly.LoadFile(openFile.FileName);
                var assemblyNode = new TreeNode(assembly.GetName().Name);
                this.treeView1.Nodes.Add(assemblyNode);
                //module
                var modules = assembly.GetModules();
                //foreach (var module in modules)
                foreach (var module in modules)
                {   //module view 
                    var moduleNode = new TreeNode(module.Name);
                    assemblyNode.Nodes.Add(moduleNode);
                    //reference 
                    var referenceNode = new TreeNode("Reference");
                    moduleNode.Nodes.Add(referenceNode);
                    var refers = assembly.GetReferencedAssemblies();
                    foreach (var refer in refers)
                    {
                        var referNode = new TreeNode(refer.Name);
                        referenceNode.Nodes.Add(referNode);
                    }
                    //types
                    Type[] typearr = null;
                    try
                    {
                        typearr= module.GetTypes();
                    }
                    catch(ReflectionTypeLoadException ex)
                    {
                        typearr = ex.Types;
                    }
                    //字典 分离键值对key-value(mul)
                    var typeGroup = new System.Collections.Generic.Dictionary <String, List<Type>>();
                    foreach (var type in typearr)
                    {
                        if (type == null) continue;
                        string np = null;
                        try
                        {
                            np = type.Namespace;
                        }
                        catch { }
                        
                        if (String.IsNullOrEmpty(np))
                        {
                            np = "-";
                        }
                        if (typeGroup.ContainsKey(np))
                        {
                            typeGroup[np].Add(type);
                            continue;

                        }
                        var list = new List<Type>();
                        list.Add(type);
                        typeGroup.Add(np, list);
                    }
                    //显示namespace和class
                        foreach (var typeItem in typeGroup)
                        {
                          var nameSpaceNode = new TreeNode(typeItem.Key);
                          moduleNode.Nodes.Add(nameSpaceNode);

                           foreach ( var type in typeItem.Value)
                           { 
                            var classNode = new TreeNode(type.Name);
                                nameSpaceNode.Nodes.Add(classNode);
                           
                            //construc
                            var constructInfos = type.GetConstructors(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static|BindingFlags.DeclaredOnly|BindingFlags.Instance);

                            ParameterInfo[] consParameters = null;
                            String parsList = String.Empty;
                            try
                            {


                                foreach (var cons in constructInfos)
                                {
                                    consParameters = cons.GetParameters();
                                    var conlist = new List<String>();
                                    foreach (var consParameter in consParameters)
                                    {
                                        conlist.Add(consParameter.ParameterType.Name);
                                    }
                                    parsList = String.Join(" , ", conlist.ToArray());

                                    var constructNode = new TreeNode(cons.Name + "(" + parsList + ")");
                                    classNode.Nodes.Add(constructNode);
                                }
                            }
                            catch { }
                            //method
                            var methods = type.GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static|BindingFlags.DeclaredOnly|BindingFlags.Instance);
                            foreach (var method in methods) 
                            {
                                var methodName = method.Name;
                                if (method.IsSpecialName)
                                {
                                    continue;
                                }

                                string paramStr = string.Empty;
                                String methodRT = null;
                                try
                                {
                                    var parameters = method.GetParameters();
                                    var paraList = new List<String>();
                                    foreach (var parameter in parameters)
                                    {
                                        paraList.Add(parameter.ParameterType.Name);
                                    }
                                    paramStr = String.Join(", ", paraList.ToArray());
                                    methodRT = method.ReturnType.Name;
                                }
                                catch {
                                    paramStr = "UnKnow";
                                    methodRT = "methodRT_UnKnow";
                                }
                              
                                var methodNode = new TreeNode(method.Name + "(" + paramStr + ")" + " : " + methodRT);
                                classNode.Nodes.Add(methodNode);   
                            }
                            //property
                            var proInfos = type.GetProperties();

                            foreach (var proinfo in proInfos)
                            {
                                TreeNode proNode = null;
                                try
                                {
                                    proNode = new TreeNode(proinfo.Name + " : " + proinfo.PropertyType.Name);
                                    classNode.Nodes.Add(proNode);
                                }
                                catch {
                                    proNode = new TreeNode(proinfo.Name + " : " + "UnKnow");
                                    classNode.Nodes.Add(proNode);
                                }

                                MethodInfo  promethods = null;
                                ParameterInfo[] proParameters = null;
                               List<String> proList = null;
                                String parsStr2 = String.Empty;
                                String prt = null;
                                if (proinfo.CanRead)
                                {
                                    try
                                    {
                                        promethods = proinfo.GetMethod;
                                        proParameters = promethods.GetParameters();
                                         proList = new List<String>();
                                        foreach (var proParameter in proParameters)
                                        {
                                            proList.Add(proParameter.ParameterType.Name);
                                        }
                                        parsStr2 = string.Join(", ", proList.ToArray());
                                        prt = promethods.ReturnType.Name;
                                    }
                                    catch { }
                                    var proInfoNode = new TreeNode(promethods.Name + "(" + parsStr2 + ")" + prt );
                                    proNode.Nodes.Add(proInfoNode);
                                }
                                if (proinfo.CanWrite)
                                {  try
                                    {

                                        promethods = proinfo.GetMethod;
                                        proParameters = promethods.GetParameters();
                                        proList = new List<String>();
                                        foreach (var proParameter in proParameters)
                                        {
                                            proList.Add(proParameter.ParameterType.Name);
                                        }
                                    }
                                    catch { }
                                    var proInfoNode = new TreeNode(promethods.Name + "(" + string.Join(", ", proList.ToArray()) + ")" + promethods.ReturnType.Name);
                                    proNode.Nodes.Add(proInfoNode);
                                }

                                //filds 
                                var filds = type.GetFields(BindingFlags.Public |
                                                           BindingFlags.Static |
                                                           BindingFlags.NonPublic |
                                                           BindingFlags.Instance |
                                                           BindingFlags.DeclaredOnly);
                                try
                                {
                                    foreach (var fildsInfo in filds)
                                    {
                                        var fildNode = new TreeNode(fildsInfo.Name + ": " + fildsInfo.FieldType.Name);
                                        classNode.Nodes.Add(fildNode);
                                    }
                                }
                                catch { }
                            }
                           

                           }
                        }
                    
                }
            }
        }
    }
}

技术分享

c# aseembly 反射.dll文件获取源代码的结构

原文:http://www.cnblogs.com/kongbo/p/7275299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!