📄 23.7.txt
字号:
Listing 23.7 Loading Assemblies and Invoking Methods
using System;
using System.IO;
using System.Reflection;
namespace _10_DynamicInvocation
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
if( args.Length < 0 )
{
ShowUsage();
return;
}
if( Directory.Exists( args[0] ) == false)
{
Console.WriteLine( “Cannot find specified directory.” );
return;
}
// enumerate all dll files in directory
foreach (string fileName in Directory.GetFiles( args[0], “*.dll” ))
{
// load assembly
Assembly curAsm = Assembly.LoadFile( fileName );
// enumerate each type in assembly
foreach( Type t in curAsm.GetTypes() )
{
// look for MakeHoarse method
MethodInfo hoarseMethod = t.GetMethod( “MakeHoarse” );
if( hoarseMethod == null )
continue;
// create object and invoke method
object curObj = curAsm.CreateInstance( t.Name );
hoarseMethod.Invoke( curObj, null );
}
}
}
static void ShowUsage()
{
Console.WriteLine( “Usage: animalfarm.exe <directory>” );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -