Create types of objects locally or remotely, or obtain references to existing remote objects. : Activator « Reflection « C# / C Sharp






Create types of objects locally or remotely, or obtain references to existing remote objects.

 

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

public class SomeType
{
    public void DoSomething(int x)
    {
        Console.WriteLine(x);
    }
}

public class Example
{
    static void Main()
    {
        Object o = Activator.CreateInstance(typeof(StringBuilder));
        StringBuilder sb = (StringBuilder) o;
        sb.Append("Hello, there.");
        Console.WriteLine(sb);

        System.Runtime.Remoting.ObjectHandle oh = Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, typeof(SomeType).FullName);

        SomeType st = (SomeType) oh.Unwrap();

        st.DoSomething(5);
    }
}

   
  








Related examples in the same category

1.System.Activator
2.ActivationContext identifies the activation context for the current application.
3.Invoke a member with Activator