PC Review


Reply
Thread Tools Rate Thread

How to create an object instance by its string type?

 
 
=?Utf-8?B?U2hhcm9u?=
Guest
Posts: n/a
 
      26th Jun 2006
I have a list of System.Type and I wish to create an instance for each of
this types.

string[] types = "System.Boolean", "System.Int32", "System.Single",
"System.Double", "System.String", "System.DateTime"};

System.Type type;

for( int i=0; i<types .Length; ++i )
{
type = System.Type.GetType(rowTypes[i]);

// Missing code in here...
}


Can anybody now how can I do that?


---------
Thanks
Sharon
 
Reply With Quote
 
 
 
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      26th Jun 2006
Sharon,

You either use reflection or you can use Activator.CreateInstance method
providing the Type object or strings for the type name and declaring
assembly.

With reflection you need to call Type.GetContructor and then call Invoke
method on the returned ConstructorInfo object.


--
HTH
Stoitcho Goutsev (100)
You can either get
"Sharon" <(E-Mail Removed)> wrote in message
news:F751CDB2-76E1-4B72-9433-(E-Mail Removed)...
>I have a list of System.Type and I wish to create an instance for each of
> this types.
>
> string[] types = "System.Boolean", "System.Int32", "System.Single",
> "System.Double", "System.String", "System.DateTime"};
>
> System.Type type;
>
> for( int i=0; i<types .Length; ++i )
> {
> type = System.Type.GetType(rowTypes[i]);
>
> // Missing code in here...
> }
>
>
> Can anybody now how can I do that?
>
>
> ---------
> Thanks
> Sharon



 
Reply With Quote
 
Claes Bergefall
Guest
Posts: n/a
 
      26th Jun 2006
Try Activator.CreateInstance

/claes

"Sharon" <(E-Mail Removed)> wrote in message
news:F751CDB2-76E1-4B72-9433-(E-Mail Removed)...
>I have a list of System.Type and I wish to create an instance for each of
> this types.
>
> string[] types = "System.Boolean", "System.Int32", "System.Single",
> "System.Double", "System.String", "System.DateTime"};
>
> System.Type type;
>
> for( int i=0; i<types .Length; ++i )
> {
> type = System.Type.GetType(rowTypes[i]);
>
> // Missing code in here...
> }
>
>
> Can anybody now how can I do that?
>
>
> ---------
> Thanks
> Sharon



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can I create an object instance from a string =?Utf-8?B?VGhlU2ViYXN0ZXI=?= Microsoft C# .NET 3 26th Jul 2006 02:11 PM
Create an instance from type as string boxim Microsoft C# .NET 3 9th Jan 2004 06:14 PM
Cannot create an object of type 'System.String[]' from its string representation 'String[] Array' for the 'Options' property. Hessam Microsoft C# .NET 0 8th Aug 2003 09:45 AM
Cannot create an object of type 'System.String[]' from its representation 'String[] Array' Hessam Microsoft Dot NET Framework 0 8th Aug 2003 09:36 AM
Cannot create an object of type 'System.String[]' from its representation 'String[] Array' Hessam Microsoft ASP .NET 0 8th Aug 2003 09:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:13 AM.