Dynamic object creation with choice of constructor

  • Thread starter Thread starter Nak
  • Start date Start date
N

Nak

Hi there,

I'm using Activator.CreateInstance to create an object from a string,
unfortunately it requires that I have a constructor with no parameters (at
least I presume)

I was wondering if anyone knew how to do the following.

A) Create a type object from a string
B) Create an instance of this type using a constructor with
parameters

Thanks loads in advance!!!!!

Nick.
 
Nak said:
A) Create a type object from a string

For example:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///
B) Create an instance of this type using a constructor with
parameters

Notice that 'CreateInstance' is overloaded:

..NET Framework Class Library -- 'Activator.CreateInstance Method (Type,
Object[])'
<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemactivatorclasscreateinstancetopic4.asp>
 
Hi Herfried,

Excellent! Cheers for the help! :-)

Nick.

Herfried K. Wagner said:
Nak said:
A) Create a type object from a string

For example:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///
B) Create an instance of this type using a constructor with
parameters

Notice that 'CreateInstance' is overloaded:

.NET Framework Class Library -- 'Activator.CreateInstance Method (Type,
Object[])'
<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemactivatorclasscreateinstancetopic4.asp>
 
Back
Top