PC Review


Reply
Thread Tools Rate Thread

how to create instance by type

 
 
=?Utf-8?B?QWxleCBLLg==?=
Guest
Posts: n/a
 
      8th May 2007
Hi all

Is there any way to create an instance of a class using its type?
I mean something like this:

private void util() {
object a = null;

a = CreateObjectByType(typeof(MyClassA));
}

private object CreateObjectByType(Type classType) {
...
}

Thank you

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      8th May 2007
Alex,

Yes, you want to take a look at the static CreateInstance method on the
Activator class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Alex K." <(E-Mail Removed)> wrote in message
news:8AB07788-228A-42B0-A763-(E-Mail Removed)...
> Hi all
>
> Is there any way to create an instance of a class using its type?
> I mean something like this:
>
> private void util() {
> object a = null;
>
> a = CreateObjectByType(typeof(MyClassA));
> }
>
> private object CreateObjectByType(Type classType) {
> ...
> }
>
> Thank you
>



 
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
Create new Form of the same Type as an existing Instance tarnap Microsoft C# .NET 5 21st Sep 2006 10:41 AM
How to create an object instance by its string type? =?Utf-8?B?U2hhcm9u?= Microsoft C# .NET 2 26th Jun 2006 03:51 PM
Get Type Of Class - Create Instance? Mythran Microsoft C# .NET 2 21st Mar 2006 11:39 PM
Could not create type instance Brian.ONeil@MCDean.com Microsoft Dot NET Compact Framework 1 16th Dec 2005 06:49 PM
Create an instance from type as string boxim Microsoft C# .NET 3 9th Jan 2004 06:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:23 AM.