Re: how do i access methods when the .net assembly was loaded with late biding?
From: richlm (rich_lm_at_h0tmai1.com)
Date: 11/30/04
- Next message: Cor Ligthert: "Re: printing excel file without using automation"
- Previous message: Jon Skeet [C# MVP]: "Re: Problem reading Chr(0) from random file..."
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 30 Nov 2004 11:33:39 +0100
There are several approaches to invoke a method using reflection. Take a
look at the following .NET framework classes/methods on MSDN/VS.NET-help:
- System.Activator.CreateInstance()
- System.Type.InvokeMember()
- System.Type.GetMember()/GetMembers()
- System.Reflection.MethodInfo/MethodBase
Here's a code sample (console application) that loads itself dynamically and
invokes ToString() on the types it finds:
using System;
using System.Reflection;
class Class1
{
public static void Main()
{
Assembly SampleAssembly =
Assembly.LoadFrom(@"c:\dev\test\bin\debug\test.exe");
Type[] Types = SampleAssembly.GetTypes();
foreach (Type oType in Types)
{
object instance = Activator.CreateInstance(oType);
string result = (string) oType.InvokeMember( "ToString",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod,
null, instance, null );
Console.WriteLine(result);
}
}
}
class class2
{
public class2() {}
public override string ToString()
{
return "hello from class2";
}
}
- Next message: Cor Ligthert: "Re: printing excel file without using automation"
- Previous message: Jon Skeet [C# MVP]: "Re: Problem reading Chr(0) from random file..."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|