Click here to Skip to main content
16,009,652 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
what is the exact use of activator class???

C#
 Type ClassType = Type.GetType(ClassName + ", TMS.API, Version=1.0.0.0, Culture=neutral,  Custom=null");
            try
            {
                // Open connection
                OpenConnection();
                // Create the command
                //Execute the SP and return Reader
                dataReaderToFill = ExecuteReaderForXML(spName, baseObject.GetXML());
                int ColumnLength = dataReaderToFill.FieldCount;
                while (dataReaderToFill.Read())
                {
                    Object ClassObject = Activator.CreateInstance(ClassType);

...
Posted
Updated 24-Jun-13 6:31am
v2
Comments
[no name] 24-Jun-13 12:31pm    
You will get along much better if you just start reading the documentation instead of these endless "what is x used for" postings. http://msdn.microsoft.com/en-us/library/system.activator.aspx

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


System.Activator[^]

In the case above, its being used to create a class based on the type of the class. The type of the class is created dynamically in the first line.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 24-Jun-13 12:53pm    
5ed. ThePhantomUpvoter correctly pointed out that such questions are not productive.
—SA
Activator is part of the reflection stuff built into .Net, and as such, is good for dynamically instantiating a class.

If you're using the factory pattern, Activator is your best friend.

I frequently use it for process-handler factories (don't confuse this with HTTP handlers, they're different). I build an abstract base class and some processes that use that base class, calling its abstract methods to get something done. Then I put the fully-qualified type name of the child classes into a database or other configuration, and instantiate from that.

Typically it goes something like this:

C#
// do something to get type name here, for now, just pretend it's stored in childClassName
Type t = Type.GetType(childClassName);
FooBaseClass fbc = (FooBaseClass)Activator.CreateInstance(t);
// fbc is ready to use as an instance of a child class of FooBaseClass


That's the core of a configuration-driven factory.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900