下面的代码将名为“example.exe”或“example.dll”的程序集加载到当前应用程序域中,从该程序集获取名为 Example 的类型,为该类型获取名为 MethodA 的无参数方法,然后执行该方法。有关从所加载程序集中获取信息的完整讨论,请参见动态加载和使用类型。
Imports System
Imports System.Reflection
Public Class Asmload0
Public Shared Sub Main()
' Use the file name to load the assembly into the current
' application domain.
Dim a As [Assembly] = [Assembly].Load("example")
' Get the type to use.
Dim myType As Type = a.GetType("Example")
' Get the method to call.
Dim mymethod As MethodInfo = myType.GetMethod("MethodA")
' Create an instance
Dim obj As Object = Activator.CreateInstance(myType)
' Execute the method.
mymethod.Invoke(obj, Nothing)
End Sub
End Class
using System;
using System.Reflection;
public class Asmload0
{
public static void Main ()
{
// Use the file name to load the assembly into the current
// application domain.
Assembly a = Assembly.Load("example");
// Get the type to use.
Type myType = a.GetType("Example");
// Get the method to call.
MethodInfo mymethod = myType.GetMethod("MethodA");
// Create an instance.
Object obj = Activator.CreateInstance(myType);
// Execute the method.
mymethod.Invoke(obj,null);
}
}
using namespace System;
using namespace System::Reflection;
void main()
{
// Use the file name to load the assembly into the current
// application domain.
Assembly^ a = Assembly::Load("example");
// Get the type to use.
Type^ myType = a->GetType("Example");
// Get the method to call.
MethodInfo^ mymethod = myType->GetMethod("MethodA");
// Create an instance.
Object^ obj = Activator::CreateInstance(myType);
// Execute the method.
mymethod->Invoke(obj,nullptr);
}