首页 > 代码库 > C#控制台 assembly与getmethod获取一个dll中的类的公有函数名

C#控制台 assembly与getmethod获取一个dll中的类的公有函数名

1 dll中有两个类

 

class1

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ClassLibrary1
 8 {
 9     public class Student
10     {
11         private int _ageStudent;
12         private string _nameStudent;
13         public int AgeStudent { get; set; }
14         public int NameStudent { get; set; }
15         public void HelloWorldStudent() { Console.WriteLine("hello world,i am a Student"); }
16 
17     }
18 }

 

 

 

 

class2

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ClassLibrary1
 8 {
 9     public class Teacher
10     {
11         private int _ageTeacher;
12         private string _nameTeacher;
13         public int AgeTeacher { get; set; }
14         public int NameTeacher { get; set; }
15         public void HelloWorldTeacher(){Console.WriteLine("hello world,i am a teacher");}
16     }
17 }

 

 

 

2 code

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Reflection;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace ConsoleApplication13
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             string path = @"D:\用户目录\我的文档\visual studio 2015\Projects\ConsoleApplication13\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
15             Assembly dllFile = Assembly.LoadFile(path);
16             Type[] classType = dllFile.GetTypes();
17 
18             foreach (var item in classType)
19             {
20                 //item现在是类
21                 Console.WriteLine(item.Name);
22 
23                 //类下面有许多的函数成员
24                 MethodInfo[] methods = item.GetMethods();
25                 foreach (var itemOfMethod in methods)
26                 {
27                     Console.WriteLine(itemOfMethod.Name);
28                 }
29                 //为了好看
30                 Console.WriteLine();
31             }
32 
33             Console.ReadKey();
34         }
35     }
36 }

 

 

 

3 show

技术分享

 

C#控制台 assembly与getmethod获取一个dll中的类的公有函数名