Click here to Skip to main content
13,298,088 members (73,341 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 2 May 2012

Alternative ways to instantiate objects

, 1 May 2012
Rate this:
Please Sign up or sign in to vote.
Introduction for beginners on how to dynamically instantiate external types


In the vast majority of times when we are coding an application, we use the keyword "new" to instantiate an object, as shown below:
Dim obj As New AnyObject
In this article we will see two other ways on how to <code>dynamically instantiate objects without using the keyword "new".  


In a project that I recently participated one of the requirements was to instantiate objects dynamically based System.Types external to the application. 

It was a <span class="hps">UserControl</span> whose purpose was to display in a <span class="hps">PropertyGrid</span> properties of the instantiated object so that only the properties modified by the user in the PropertyGrid, were persisted and subsequently reapplied to the object whenever a new instance of it was created. 

The System.Type to be loaded was provided by the user by informing the assembly location on disk and the System.Type to instantiate

During the tests phase, I've used other assemblies created by me in other projects and also assemblies created by others to make sure it was actually working properly regardless of the class.

During this period, I found some situations in which the instantiation via reflection would not work. 

Instantiating through reflection 

 This type of instantiation is widely used by most programmers who need to dynamically create instances of System.Type 's external to the application.

By using Assembly.CreateInstance(string) to instantiate a type all the instantiation process occurs normally as if you were using the keyword "new", that is the constructor of the class is executed and all its fields gets initialized properly. 

Dim obj1 As Object = _assembly.CreateInstance("MyExternalAssembly.AnyObject"

In the example above we are instantiating a class called "AnyObject" on the assembly called "MyExternalAssembly". 

In this case you must provide a string with the full name of the <span class="hps">System.Type</span> including the assembly name and namespace (if any), and the name of the class also it must exist in <span class="hps alt-edited">the</span> <span class="hps alt-edited"></span> <span class="hps alt-edited"></span> <span class="hps alt-edited">_assembly </span>object, otherwise the instantiation does not occur.

Note: Internally after check if the provided System.Type string exists in the loaded assembly, it runs the <span class="hps">Activator.CreateInstance</span><span class="hps atn">(</span><span>System.Type)</span> method that returns the instance to the caller. 

Instantiating through Serialization/Deserialization  

I decided tocall this kind of instantiation as "serialization / deserialization" due to the fact that its being used internally in the .Net Framework to deserialize <span class="hps">WCF</span> <span class="hps">DataContracts</span>.

For a few times during the testing phase in the project I participated in, I came across situations where logic in the constructor of the class were preventing the instantiation of the object, especially when it was a commercial component whose licensing mechanisms were located in the constructors of the class and were firing  exceptions when licensing rules were violated. 

Note: In my opinion a good mechanism for licensing and validation must rely not only on the constructor of a class, but also in key methods of the class, and other classes internal to it. 

In cases like this instantiating a class by using Reflection is not possible since via Reflection and also  through the <span class="hps">keyword</span> "new", the constructor always runs during instantiation, preventing it from complete. 

By analyzing the .Net framework code through Reflector, I found that the <span class="hps">WCF's </span>DataContracts when deserializing an object were never invoking any class's constructors, as well as their fields were not initialized with "default" values that eventually were defined on it. 

The reason why the fields are not initialized makes sense. From what I understand is a way to gain performance, since during deserialization - after object creation - these fields will receive different values other than the "Defaults", that way it doesn't make any sense if they were assigned twice, once with their "defaultvalues ??and another with the values ??of serialization. 

I still do not understand why the constructor is not invoked, perhaps for performance reasons as well? I do not know ... 

Below is the code that does the instantiation without running the constructors or initializing fields:  

Dim type = _assembly.GetType("MyExternalAssembly.OtherObject")
Dim obj1 As Object = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type) 

Note that the method <span class="hps">GetUninitializedObject</span><span class="hps">()</span> takes a System.Type that can be a System.Type from an existing class in the assembly itself or an external assembly, however the external assembly should be previously loaded. 

In the example above we use a System.Type from an assembly external to the application, whose constructor throws an InvalidOperationException, therefore we can never instantiate an object of this class if we use the standard instantiation by using the keyword "new" or a instantiation via Reflection  

Using the source code 

In the solution of the source code of this article there are two projects, a <span class="hps">Class Library</span> and a <span class="hps">Windows Form</span> <span class="hps">Application</span>. 

The "MyExternalAssembly" is a Class Library which has two classes, one is a class called "AnyObject" that allows any instantiation mentioned in this article to be used, the other class is called "otherObject" which is a class whose default constructor fires one exception, therefore it can be instantiated only by using the process "serialization/deserialization" presented here. 

The Project <span class="hps">PropertyInspector</span> is a <span class="hps">Windows Forms</span> <span class="hps">Application</span> that allows you to select an external assembly - in this case the assembly MyExternalAssembly - and display your fields in a PropertyGrid, as follows: 




The purpose of this article is to show to beginners alternative ways to instantiate objects and not neither to elect the best way to instantiate an object nor the one that is "more correct"

Perhaps there are other ways to instantiate objects in .NET I may not know yet, if you do, please, feel free to share it with us by using the session "Comments and Discussions". 

Depending on usage, one method may be more advantageous than the other in terms of gains in performance, usability, etc.  


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


About the Author

Ana Carolina Zambon
Software Developer (Senior)
Brazil Brazil
I am a software developer since I was 13.
In the 90s I started developing for my own BBS (Bulletin Board System) which was run under IBM OS/2 and I needed to extend it in order go provide more services for the paid users.
Since them I started to get to know other programming languages like Basic, C, Clipper, Clipper Summer, DBase, PPLC, ASP3, Visual Basic, SQL and .NET
Despite knowing other languages​​, I am a big fan and enthusiastic in .NET development, specially the intrinsics of Reflection.
I spent the last couple of years focused on creating backoffice/backstage applications.
I'm a N-tier application developer, but I am focused on the development of components and frameworks for business rules and data access.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Jackson Savitraz13-Oct-14 12:02
professionalJackson Savitraz13-Oct-14 12:02 
GeneralI find it nice for a start. Pin
eferreyra19-Jun-13 9:05
membereferreyra19-Jun-13 9:05 
Questionnot bad Pin
CIDev6-Jun-12 7:35
memberCIDev6-Jun-12 7:35 
GeneralActivator.CreateInstance Pin
bensonxiong7-May-12 20:21
memberbensonxiong7-May-12 20:21 
GeneralRe: Activator.CreateInstance Pin
Carolina Zambon9-May-12 9:22
memberCarolina Zambon9-May-12 9:22 
GeneralAbout ctors on deserialization Pin
tgrt7-May-12 12:27
membertgrt7-May-12 12:27 
GeneralRe: About ctors on deserialization Pin
Carolina Zambon9-May-12 9:21
memberCarolina Zambon9-May-12 9:21 
QuestionI expected more ways to instantiate an object. Pin
Paulo Zemek7-May-12 6:30
mvpPaulo Zemek7-May-12 6:30 
When I saw the title I expected to see many ways to instantiate an object.

In fact, I see only 2:
Activator.CreateInstance and GetUnitializedObject.
But there are more.
From the Type itself we can get all the constructors, and call Invoke on one of the constructors (so, it is also using Reflection, but it is not using the Activator class).

Also, we can create a Expression that does a new, compile it, and then call the delegate. This is slow if you use it only once, but if you are going to create thousands of instances of a given type, the compiled delegate is the fastest one compared to the reflection ones, and really works like the new keyword (the GetUnitializedObject, well, is always unitialized).

So, if you could give some more options, I can very well vote a 5 for this article.
SuggestionRe: I expected more ways to instantiate an object. Pin
Philip Liebscher7-May-12 10:33
memberPhilip Liebscher7-May-12 10:33 
GeneralRe: I expected more ways to instantiate an object. Pin
Carolina Zambon9-May-12 9:14
memberCarolina Zambon9-May-12 9:14 
SuggestionRe: I expected more ways to instantiate an object. Pin
Philip Liebscher9-May-12 9:26
memberPhilip Liebscher9-May-12 9:26 
GeneralRe: I expected more ways to instantiate an object. Pin
Carolina Zambon9-May-12 9:36
memberCarolina Zambon9-May-12 9:36 
QuestionInstantiating through Serialization/Deserialization Pin
FatCatProgrammer3-May-12 6:04
memberFatCatProgrammer3-May-12 6:04 
AnswerRe: Instantiating through Serialization/Deserialization Pin
Carolina Zambon3-May-12 16:59
memberCarolina Zambon3-May-12 16:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 2 May 2012
Article Copyright 2012 by Ana Carolina Zambon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid