Re: how do i access methods when the .net assembly was loaded with late biding?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: richlm (rich_lm_at_h0tmai1.com)
Date: 11/30/04


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";
 }
}
 



Relevant Pages

  • RE: System.Reflection.TargetInvokeException
    ... A TargetException is thrown when an attempt is made to invoke a non-static ... public class AppClass ... public static void main ... Object ptInstance = Activator.CreateInstance; ...
    (microsoft.public.dotnet.vjsharp)
  • Re: Thread updating UI from other Classes...
    ... it exists at the instance level. ... public class ClassWithStaticMethod ... public static void SomeMethod() ... invoke it is use the class name: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Threading - Problem with Invoking between forms.
    ... Look into using the AsyncOperation and AsyncOperationManager classses ... I found a code sample that he posted here ~ ... (which has ALOT of things going on it in) ... My problem is from my new class i can't use the Invoke() method. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Custom xpath function, ResolveFunction called but not Invoke..
    ... being called but Invoke() isn't. ... I see that the ReturnType property is called a couple ... Consider to post a minimal but complete code sample that allows us to ... If you use XmlDocument then there is only one root node and one root ...
    (microsoft.public.dotnet.xml)
  • Re: Cannot invoke main method from assembly via reflection
    ... (You're invoking a static method, so there's no object instance to invoke it ... Jim ...
    (microsoft.public.dotnet.languages.csharp)