Invoke a member with Activator : Activator « Reflection « C# / C Sharp






Invoke a member with Activator

 

using System;
using System.Reflection;
using System.Text;

public class Example
{
   public static void Main()
   {
      Type sbType = typeof(StringBuilder);
      object o = Activator.CreateInstance(sbType);

      sbType.InvokeMember("Append", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, 
         Type.DefaultBinder, o, new object[] {"Hello, there."});
      Console.WriteLine(o.ToString());

      o = Activator.CreateInstance(sbType, new object[]{"Hi"});

      sbType.InvokeMember("Append", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, Type.DefaultBinder, o, new object[] {" hello!"});
      Console.WriteLine(o.ToString());
   }
}

   
  








Related examples in the same category

1.System.Activator
2.ActivationContext identifies the activation context for the current application.
3.Create types of objects locally or remotely, or obtain references to existing remote objects.