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