Пример #1
0
        /// <summary>
        /// 获取程序集名称列表
        /// </summary>
        public AssemblyResult GetAssemblyName(String path)
        {
            AssemblyResult result = new AssemblyResult();

            string[] dicFileName = Directory.GetFileSystemEntries(path);
            if (dicFileName != null)
            {
                List <string> assemblyList = new List <string>();
                foreach (string name in dicFileName)
                {
                    assemblyList.Add(name.Substring(name.LastIndexOf('/') + 1));
                }
                result.AssemblyName = assemblyList;
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 获取程序集中的类名称
        /// </summary>
        /// <param name="assemblyName">程序集</param>
        public AssemblyResult GetClassName(string assemblyName)
        {
            AssemblyResult result = new AssemblyResult();

            if (!String.IsNullOrEmpty(assemblyName))
            {
                // assemblyName = assemblyName;// path +
                Assembly      assembly  = Assembly.LoadFrom(assemblyName);
                Type[]        ts        = assembly.GetTypes();
                List <string> classList = new List <string>();
                foreach (Type t in ts)
                {
                    //classList.Add(t.Name);
                    classList.Add(t.FullName);
                }
                result.ClassName = classList;
            }
            return(result);
        }
Пример #3
0
        public String GetMethodInfo(string assemblyName, string className, String methodname)
        {
            AssemblyResult result = new AssemblyResult();
            StringBuilder  sb     = new StringBuilder();

            if (!String.IsNullOrEmpty(assemblyName) && !String.IsNullOrEmpty(className))
            {
                //  assemblyName = path + assemblyName;
                Assembly assembly = Assembly.LoadFrom(assemblyName);
                Type     type     = assembly.GetType(className, true, true);
                if (type != null)
                {
                    //类的方法
                    List <string> methods     = new List <string>();
                    MethodInfo[]  methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    foreach (MethodInfo mi in methodInfos)
                    {
                        methods.Add(mi.Name);
                        //方法的参数
                        int csgs = 0;
                        if (mi.Name.Equals(methodname))
                        {
                            foreach (ParameterInfo p in mi.GetParameters())
                            {
                                csgs++;
                                string csm = p.Name;

                                string cslx = p.ParameterType.ToString();
                                object o    = p.DefaultValue;
                                sb.Append("参数名:" + csm + Environment.NewLine);
                                sb.Append("参数类型:" + cslx + Environment.NewLine);
                                sb.Append("默认值:" + o.ToString() + Environment.NewLine);
                            }
                        }
                        //方法的返回值
                        //string returnParameter = mi.ReturnParameter.ToString();
                    }
                    result.Methods = methods;
                }
            }
            return(sb.ToString());
        }
Пример #4
0
        /// <summary>
        /// 获取类的属性、方法
        /// </summary>
        /// <param name="assemblyName">程序集</param>
        /// <param name="className">类名</param>
        public AssemblyResult GetClassInfo(string assemblyName, string className)
        {
            AssemblyResult result = new AssemblyResult();

            if (!String.IsNullOrEmpty(assemblyName) && !String.IsNullOrEmpty(className))
            {
                //  assemblyName = path + assemblyName;
                Assembly assembly = Assembly.LoadFrom(assemblyName);
                Type     type     = assembly.GetType(className, true, true);
                if (type != null)
                {
                    //类的属性
                    List <string>  propertieList = new List <string>();
                    PropertyInfo[] propertyinfo  = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    foreach (PropertyInfo p in propertyinfo)
                    {
                        propertieList.Add(p.ToString());
                    }
                    result.Properties = propertieList;

                    //类的方法
                    List <string> methods     = new List <string>();
                    MethodInfo[]  methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    foreach (MethodInfo mi in methodInfos)
                    {
                        methods.Add(mi.Name);
                        //方法的参数
                        //foreach (ParameterInfo p in mi.GetParameters())
                        //{

                        //}
                        //方法的返回值
                        //string returnParameter = mi.ReturnParameter.ToString();
                    }
                    result.Methods = methods;
                }
            }
            return(result);
        }
        private void menuopen_Click(object sender, EventArgs e)
        {
            opendllFileDialog.Filter = "测试类库dll文件(*.DLL)|*.dll";
            this.opendllFileDialog.ShowDialog();
            string path = this.opendllFileDialog.FileName;

            this.dlltree.CheckBoxes = true;
            AssemblyHandler ah = new AssemblyHandler();

            if (!String.IsNullOrEmpty(path))
            {
                Assembly assembly = Assembly.LoadFile(path);
                pubfolder = Path.GetDirectoryName(path) + "\\";
                //MessageBox.Show(pubfolder);
                this.Statuslbl.Text = "类库位置 :" + path;
                try
                {
                    AssemblyResult ar = ah.GetClassName(path);
                    this.lablpath.Text = path;
                    this.dlltree.Nodes.Clear();

                    //  thand();
                    for (int i = 0; i < ar.ClassName.Count; i++)
                    {
                        try
                        {
                            string         strcls  = ar.ClassName[i];
                            AssemblyResult clsinfo = ah.GetClassInfo(path, strcls);
                            //
                            List <TreeNode> lst = new List <TreeNode>();

                            if (clsinfo.Methods.Count > 0)
                            {
                                for (int ii = 0; ii < clsinfo.Methods.Count; ii++)
                                {
                                    Application.DoEvents();
                                    object obj = assembly.CreateInstance(strcls); // 创建类的实例
                                    Type   t   = obj.GetType();
                                    try
                                    {
                                        MethodInfo minfo = t.GetMethod(clsinfo.Methods[ii]);
                                        IList <System.Reflection.CustomAttributeData> lstsx = minfo.GetCustomAttributesData();

                                        if (lstsx != null && lstsx.Count > 0)
                                        {
                                            for (int k = 0; k < lstsx.Count; k++)
                                            {
                                                object O  = (object)lstsx[k];
                                                string OX = O.ToString();
                                                // (lstsx)).Items[0].AttributeType	{Name = "TestAttribute" FullName = "NUnit.Framework.TestAttribute"}	System.Type {System.RuntimeType}
                                                if (OX.Equals("[NUnit.Framework.TestAttribute()]"))
                                                {
                                                    TreeNode subtree = new TreeNode();

                                                    subtree.Text = clsinfo.Methods[ii];
                                                    subtree.Tag  = ii.ToString();
                                                    if (!String.IsNullOrEmpty(clsinfo.Methods[ii]))
                                                    {
                                                        lst.Add(subtree);
                                                    }
                                                }
                                            }
                                        }
                                        //}
                                    }
                                    catch (Exception error)
                                    {
                                        //  MessageBox.Show(error.Message);
                                        continue;
                                    }
                                    // trlst[ii].Checked = true;
                                }



                                TreeNode[] treearray = new TreeNode[lst.Count];
                                for (int jj = 0; jj < lst.Count; jj++)
                                {
                                    Application.DoEvents();
                                    treearray[jj]      = new TreeNode();
                                    treearray[jj].Text = lst[jj].Text;
                                    treearray[jj].Tag  = lst[jj].Tag;
                                }


                                if (treearray.Length > 0)
                                {
                                    TreeNode tn = new TreeNode(strcls, treearray);
                                    this.dlltree.Nodes.Add(tn);
                                }
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }
                    //this.dlltree.ExpandAll();
                    // threads.Abort();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //  MessageBox.Show(error.Message);
                }
            }
        }