我正在接受采访,面试官问我下面的问题。
How to know what classes and methods are present in DLL ?
我很困惑并且说,“我们可以使用工具或重构。”
有人可以解释different ways
从DLL(from code as well as from tools
)
答案 0 :(得分:4)
我怀疑面试官是指反思。例如:
var assembly = ...; // e.g. typeof(SomeType).Assembly
var types = assembly.GetTypes();
var methods = types.SelectMany(type => type.GetMethods());
// etc
例如,您需要使用Type.IsClass
过滤类型以获取类。在使用反射查询类型或程序集的特定部分时,LINQ非常有用。请注意,上面的无参数GetMethods()
调用只会返回公共方法;您也可以指定BindingFlags
值来检索非公开方法。
答案 1 :(得分:1)
从装配中获取类型的完整路径到文件:
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...
答案 2 :(得分:0)
除此之外,您还可以使用Reflector,dotPeek或ILDASM等工具查看程序集的内容。