📄 ch6_5.cs
字号:
using System;
using System.Reflection;
using System.Collections;
class CH6_5
{
public static void Main(string[] args)
{
// If they gave us at least two arguments,
// check the assembly (arg 1) for a class
// that is derived from another class (arg 2)
if ( args.Length >= 2 )
CheckBaseClass( args[0], args[1] );
}
public static void CheckBaseClass(
string AssemblyName,
string ClassName )
{
// Step 1: Open the assembly
Assembly assembly = Assembly.LoadFrom(
AssemblyName );
if ( assembly == null )
{
Console.WriteLine("Unable to open {0}",
AssemblyName );
return;
}
// Step 2: Loop through all classes in
// the assembly
foreach ( Type t in assembly.GetTypes() )
{
// Is this a class?
if ( t.IsClass == false )
continue; // No. Skip it
// Check all of the base classes for this class, as well as
// their base classes
Type baseType = t.BaseType;
while (baseType != null)
{
if ( baseType.FullName.EndsWith(ClassName) )
{
Console.WriteLine("Class {0} is derived from {1}",
t.FullName, ClassName );
}
baseType = baseType.BaseType;
}
// Check if one of the types' interfaces is the type we're looking for
Type[] intfc = t.GetInterfaces();
foreach (Type itf in intfc)
{
if ( itf.FullName.EndsWith(ClassName) )
{
Console.WriteLine("Class {0} is derived from intf {1}",
t.FullName, ClassName );
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -