了解 DLL 中存在的所有 类 和方法有哪些不同的方法?
What are the different ways to know which all classes and methods present in a DLL?
我正在面试,面试官问了我下面的问题。
How to know what classes and methods are present in DLL ?
一头雾水说,"we can use tool or refactor that."
谁能解释一下 different ways
从 DLL (from code as well as from tools
) 中找到所有内容
我怀疑面试官说的是反思。例如:
var assembly = ...; // e.g. typeof(SomeType).Assembly
var types = assembly.GetTypes();
var methods = types.SelectMany(type => type.GetMethods());
// etc
例如,您需要使用 Type.IsClass
过滤类型以获取 类。在使用反射来查询类型或程序集的特定部分时,LINQ 非常有用。请注意,上面的无参数 GetMethods()
调用只会使用 return public 方法;您也可以指定 BindingFlags
值来检索非 public 方法。
通过文件的完整路径从程序集中获取类型:
public IEnumerable<Type> GetAllTypesInDll(string filename)
{
// load assembly from file
Assembly asm = Assembly.LoadFile(filename);
// enumerate all types
return asm.GetTypes();
}
用法:
foreach (Type type in from e in GetAllTypesInDll(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Console.exe"))
orderby e.FullName
select e)
{
// print type
Console.WriteLine("----------------");
Console.WriteLine(type.FullName);
// print type methods
Console.WriteLine("Methods:");
foreach (var mi in from e in type.GetMethods()
orderby e.Name
select e)
{
Console.WriteLine(" " + mi.Name);
}
Console.WriteLine("----------------");
}
结果:
----------------
<>f__AnonymousType0`7
Methods:
Equals
get_DisplayName
get_EMail
get_Groups
get_Login
get_Name
get_Patronymic
get_Surname
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetAllVersionsRequest
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetAllVersionsResponse
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetCurrentVersionRequest
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetCurrentVersionResponse
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetDataRequest
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetDataResponse
Methods:
Equals
GetHashCode
GetType
ToString
----------------
etc...
除此之外,您还可以使用 Reflector、dotPeek 或 ILDASM 等工具查看程序集的内容。
我正在面试,面试官问了我下面的问题。
How to know what classes and methods are present in DLL ?
一头雾水说,"we can use tool or refactor that."
谁能解释一下 different ways
从 DLL (from code as well as from tools
) 中找到所有内容
我怀疑面试官说的是反思。例如:
var assembly = ...; // e.g. typeof(SomeType).Assembly
var types = assembly.GetTypes();
var methods = types.SelectMany(type => type.GetMethods());
// etc
例如,您需要使用 Type.IsClass
过滤类型以获取 类。在使用反射来查询类型或程序集的特定部分时,LINQ 非常有用。请注意,上面的无参数 GetMethods()
调用只会使用 return public 方法;您也可以指定 BindingFlags
值来检索非 public 方法。
通过文件的完整路径从程序集中获取类型:
public IEnumerable<Type> GetAllTypesInDll(string filename)
{
// load assembly from file
Assembly asm = Assembly.LoadFile(filename);
// enumerate all types
return asm.GetTypes();
}
用法:
foreach (Type type in from e in GetAllTypesInDll(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Console.exe"))
orderby e.FullName
select e)
{
// print type
Console.WriteLine("----------------");
Console.WriteLine(type.FullName);
// print type methods
Console.WriteLine("Methods:");
foreach (var mi in from e in type.GetMethods()
orderby e.Name
select e)
{
Console.WriteLine(" " + mi.Name);
}
Console.WriteLine("----------------");
}
结果:
----------------
<>f__AnonymousType0`7
Methods:
Equals
get_DisplayName
get_EMail
get_Groups
get_Login
get_Name
get_Patronymic
get_Surname
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetAllVersionsRequest
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetAllVersionsResponse
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetCurrentVersionRequest
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetCurrentVersionResponse
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetDataRequest
Methods:
Equals
GetHashCode
GetType
ToString
----------------
----------------
ARMUpdateService.ARMGetDataResponse
Methods:
Equals
GetHashCode
GetType
ToString
----------------
etc...
除此之外,您还可以使用 Reflector、dotPeek 或 ILDASM 等工具查看程序集的内容。