PC Review


Reply
Thread Tools Rate Thread

How Can I create an object instance from a string

 
 
=?Utf-8?B?VGhlU2ViYXN0ZXI=?=
Guest
Posts: n/a
 
      26th Jul 2006
I would like to create an instance of System.Windows.Form.TextBox
but I just have the name of the objec in a string like that:

string strType = "System.Windows.Form.TextBox";

I tryed myOgject = CreateInstance(Type.GetType(strType))
but it does not work because Type.GetType(strType) rturn null.

Does anybody can help me???

--
C# est a Mozart ce que
VB est a Normand L''amour
 
Reply With Quote
 
 
 
 
TheSteph
Guest
Posts: n/a
 
      26th Jul 2006
Try this :
using System.Reflection;
//...
//...
string strTypeName = "System.Windows.Form.TextBox";
Object Obj = Activator.CreateInstance(strTypeName)


Steph


"TheSebaster" <(E-Mail Removed)> wrote in message
news:8280773C-5EB6-44A7-B691-(E-Mail Removed)...
> I would like to create an instance of System.Windows.Form.TextBox
> but I just have the name of the objec in a string like that:
>
> string strType = "System.Windows.Form.TextBox";
>
> I tryed myOgject = CreateInstance(Type.GetType(strType))
> but it does not work because Type.GetType(strType) rturn null.
>
> Does anybody can help me???
>
> --
> C# est a Mozart ce que
> VB est a Normand L''amour



 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      26th Jul 2006
Hi,

IIRC the string being passed need to have more details about the type being
instantiated, this was the reason I never used it. I have used
AppDomain.CreateInstance and it does work great


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"TheSebaster" <(E-Mail Removed)> wrote in message
news:8280773C-5EB6-44A7-B691-(E-Mail Removed)...
>I would like to create an instance of System.Windows.Form.TextBox
> but I just have the name of the objec in a string like that:
>
> string strType = "System.Windows.Form.TextBox";
>
> I tryed myOgject = CreateInstance(Type.GetType(strType))
> but it does not work because Type.GetType(strType) rturn null.
>
> Does anybody can help me???
>
> --
> C# est a Mozart ce que
> VB est a Normand L''amour



 
Reply With Quote
 
=?Utf-8?B?TWljaGFlbCBOZW10c2V2?=
Guest
Posts: n/a
 
      26th Jul 2006
The problem could be in that yoy have no assembly with TextBox loaded

use smth like this

string strType = "System.Windows.Forms.TextBox";
Assembly asm = Assembly.LoadWithPartialName("System.Windows.Forms");
Type tp = asm.GetType(strType);
object obj = Activator.CreateInstance(tp);

PS: LoadWithPartialName is depricated in .net 2.0

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche




"TheSteph" wrote:

> Try this :
> using System.Reflection;
> //...
> //...
> string strTypeName = "System.Windows.Form.TextBox";
> Object Obj = Activator.CreateInstance(strTypeName)
>
>
> Steph
>
>
> "TheSebaster" <(E-Mail Removed)> wrote in message
> news:8280773C-5EB6-44A7-B691-(E-Mail Removed)...
> > I would like to create an instance of System.Windows.Form.TextBox
> > but I just have the name of the objec in a string like that:
> >
> > string strType = "System.Windows.Form.TextBox";
> >
> > I tryed myOgject = CreateInstance(Type.GetType(strType))
> > but it does not work because Type.GetType(strType) rturn null.
> >
> > Does anybody can help me???
> >
> > --
> > C# est a Mozart ce que
> > VB est a Normand L''amour

>
>
>

 
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 to create an object instance by its string type? =?Utf-8?B?U2hhcm9u?= Microsoft C# .NET 2 26th Jun 2006 03:51 PM
'Object reference not set to an instance of an object.' when checking string length? darrel Microsoft ASP .NET 7 22nd Mar 2006 11:13 PM
creating an instance of an object(s) from a string representation. Joanne Microsoft Dot NET Compact Framework 0 24th Oct 2004 07:11 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
How To Create an instance of an object from another instance? Andro Tan Microsoft Dot NET Framework 2 21st Jul 2003 01:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:39 PM.