Activator Class in .NET 4.0

Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects

With the help of this class we can create instance of any class and also create instance of Remote object

Here is Sample code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;

 

namespace ActivatorClassSample

{

    public class Customer

    {

        public void Show(int x)

        {

            Console.WriteLine("100 / {0} = {1}", x, 100 / x);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Object o = Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase,

                                         typeof(Customer).FullName);

 

            Customer cust = (Customer)o;

            cust.Show(x);

        }

    }

}

 

Reference :- http://msdn.microsoft.com/en-us/library/system.activator.aspx

Ebook Download
View all
Learn
View all