Click here to Skip to main content
15,879,474 members
Articles / Programming Languages / Visual Basic

Alternative ways to instantiate objects

Rate me:
Please Sign up or sign in to vote.
4.76/5 (12 votes)
1 May 2012CPOL4 min read 38.5K   266   23   14
Introduction for beginners on how to dynamically instantiate external types

Introduction 

In the vast majority of times when we are coding an application, we use the keyword "new" to instantiate an object, as shown below:
VB.NET
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".  

Background  

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 UserControl whose purpose was to display in a PropertyGrid 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. 

VB.NET
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 System.Type including the assembly name and namespace (if any), and the name of the class also it must exist in the _assembly 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 Activator.CreateInstance(<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 WCF DataContracts.

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 keyword "new", the constructor always runs during instantiation, preventing it from complete. 

By analyzing the .Net framework code through Reflector, I found that the WCF's 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:  

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


Note that the method GetUninitializedObject() 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 Class Library and a Windows Form Application. 

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 PropertyInspector is a Windows Forms Application that allows you to select an external assembly - in this case the assembly MyExternalAssembly - and display your fields in a PropertyGrid, as follows: 

 

 

Summary 

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.  

License

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


Written By
Software Developer (Senior)
Brazil Brazil
Coffe-addicted software developer since her 14's

Comments and Discussions

 
GeneralMy vote of 5 Pin
Jackson Savitraz13-Oct-14 11:02
professionalJackson Savitraz13-Oct-14 11:02 
GeneralI find it nice for a start. Pin
eferreyra19-Jun-13 8:05
eferreyra19-Jun-13 8:05 
Questionnot bad Pin
BillW336-Jun-12 6:35
professionalBillW336-Jun-12 6:35 
GeneralActivator.CreateInstance Pin
bensonxiong7-May-12 19:21
bensonxiong7-May-12 19:21 
GeneralRe: Activator.CreateInstance Pin
Ana Carolina Zambon9-May-12 8:22
Ana Carolina Zambon9-May-12 8:22 
GeneralAbout ctors on deserialization Pin
tgrt7-May-12 11:27
tgrt7-May-12 11:27 
[quote]I still do not understand why the constructor is not invoked, perhaps for performance reasons as well? I do not know ...[/quote]

Serialization uses FormatterServices.GetUninitializedObject. As you noted initializing fields wouldn't make sense when the purpose of deserialization is to resconstitute the object. You can create a method with the OnDeserializingAttribute or OnDeserializedAttribute attribute if you have side effects that deserialization cannot copy.
GeneralRe: About ctors on deserialization Pin
Ana Carolina Zambon9-May-12 8:21
Ana Carolina Zambon9-May-12 8:21 
QuestionI expected more ways to instantiate an object. Pin
Paulo Zemek7-May-12 5:30
mvaPaulo Zemek7-May-12 5:30 
SuggestionRe: I expected more ways to instantiate an object. Pin
Philip Liebscher7-May-12 9:33
Philip Liebscher7-May-12 9:33 
GeneralRe: I expected more ways to instantiate an object. Pin
Ana Carolina Zambon9-May-12 8:14
Ana Carolina Zambon9-May-12 8:14 
SuggestionRe: I expected more ways to instantiate an object. Pin
Philip Liebscher9-May-12 8:26
Philip Liebscher9-May-12 8:26 
GeneralRe: I expected more ways to instantiate an object. Pin
Ana Carolina Zambon9-May-12 8:36
Ana Carolina Zambon9-May-12 8:36 
QuestionInstantiating through Serialization/Deserialization Pin
FatCatProgrammer3-May-12 5:04
FatCatProgrammer3-May-12 5:04 
AnswerRe: Instantiating through Serialization/Deserialization Pin
Ana Carolina Zambon3-May-12 15:59
Ana Carolina Zambon3-May-12 15: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.