473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Name of type to class instance?

Is it possible to create an object of a specific type just by knowing its
name as a text string? Say I have the string

System.Windows.Forms.Textbox how would I go about turing that string into an
object of that type? I think its possible with reflection, but not sure
how... thanks!
Apr 18 '07 #1
6 1006
Smokey Grindle wrote:
Is it possible to create an object of a specific type just by knowing its
name as a text string?
Almost certainly.
I think its possible with reflection, but not sure how...
As soon as you start reaching for the Reflection tool box, take a step
back and ask yourself if there's not a better way to do what you need.

What is it that you want to achieve? (never mind how, for now)

Regards,
Phill W.
Apr 18 '07 #2
Well we know for certain we have to create objects on the fly based off of a
type name specified in a database row... so right now we have the object
type... just need to create the object

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:f0**********@south.jnrs.ja.net...
Smokey Grindle wrote:
>Is it possible to create an object of a specific type just by knowing its
name as a text string?

Almost certainly.
>I think its possible with reflection, but not sure how...

As soon as you start reaching for the Reflection tool box, take a step
back and ask yourself if there's not a better way to do what you need.

What is it that you want to achieve? (never mind how, for now)

Regards,
Phill W.

Apr 18 '07 #3
On Apr 18, 8:00 am, "Smokey Grindle" <nos...@dontspamme.comwrote:
Is it possible to create an object of a specific type just by knowing its
name as a text string? Say I have the string

System.Windows.Forms.Textbox how would I go about turing that string into an
object of that type? I think its possible with reflection, but not sure
how... thanks!
Topics to look at in the documentation:

Type.GetType
Activator.CreateInstance

That should be enought to get you started.

--
Tom Shelton

Apr 18 '07 #4
awesome, thanks!

"Tom Shelton" <to*********@comcast.netwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
On Apr 18, 8:00 am, "Smokey Grindle" <nos...@dontspamme.comwrote:
>Is it possible to create an object of a specific type just by knowing its
name as a text string? Say I have the string

System.Windows.Forms.Textbox how would I go about turing that string into
an
object of that type? I think its possible with reflection, but not sure
how... thanks!

Topics to look at in the documentation:

Type.GetType
Activator.CreateInstance

That should be enought to get you started.

--
Tom Shelton

Apr 18 '07 #5
On Apr 18, 10:59 am, "Smokey Grindle" <nos...@dontspamme.comwrote:
Well we know for certain we have to create objects on the fly based off of a
type name specified in a database row... so right now we have the object
type... just need to create the object

"Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-kwrote in message

news:f0**********@south.jnrs.ja.net...
Smokey Grindle wrote:
Is it possible to create an object of a specific type just by knowing its
name as a text string?
Almost certainly.
I think its possible with reflection, but not sure how...
As soon as you start reaching for the Reflection tool box, take a step
back and ask yourself if there's not a better way to do what you need.
What is it that you want to achieve? (never mind how, for now)
Regards,
Phill W.
In addition to the others, if you are using serializable controls you
can store the searilized data into the database and then deserialize
the data back into the control. This way you don't have to worry about
setting all the properties.

Thanks,

Seth Rowe

Apr 18 '07 #6
Smokey Grindle wrote:
Well we know for certain we have to create objects on the fly based off of a
type name specified in a database row... so right now we have the object
type... just need to create the object
Ah ha!
That's one of the Good Reasons for doing this.

As Tom said, Type.GetType and Activator.CreateInstance.

This will work well for Framework-defined Types but watch out when you
start trying to use Types defined in /your own/ assemblies - whatever
process is doing all this has to be able to "get hold of" (i.e. Load)
the defining Assemblies. Can make for some fun deployment issues.

HTH,
Phill W.
Apr 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: andrew | last post by:
C:\Documents and Settings\watts\My Documents\Visual Studio Projects\boeing\showPMACfg\vuePMAcfg\vuePMAcfg.cs(88): Static member 'vuePMAcfg.pmaDataHash.pmaDataHash1' cannot be accessed with an...
11
by: Ahmet AKGUN | last post by:
Hi; is it possible to open one form in .net platform that we have its name in string ? I have string sFormName = "frmCustomer"; and I must automatically open Customer form. or is it...
2
by: blue | last post by:
I have a class called "MyClass". At runtime, I don't know that the class is of type MyClass but I do know the string name of the class. Is there a way to create an instance of a class when I only...
6
by: Rene Mansveld | last post by:
Hi, how can I create an instance (object) of a class (form) if I only know the classname (VB.NET 1.0)? I need to do this in a complex app where jobs consist of parts. Each part's data is saved...
12
by: guy lateur | last post by:
Hi all, Suppose you have this class: class foo: def bar(): Suppose you also have the strings "foo" and "bar". How can you obtain the function foo.bar()?
8
by: ABC | last post by:
How to return the class name using static getter from a class? The base class as: class abcbase { .............. public static string ObjectName {
11
by: Alexander Walker | last post by:
Hello I would like to write a method that allows me to pass a reference to an instance of a class, the name of a property of that class and a value to set that property to, the method would then...
8
by: news.microsoft.com | last post by:
How do I get the class name for a current instance. For example, if I want to know the Class Name for the current form, how do I get this programatically. Thanks
4
by: =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?= | last post by:
(using .net 2.0) Say you have a class structure like this: class Address .... end class class Person FirstName
23
by: Hugh Oxford | last post by:
How do I get an object's name? EG. $obj_FOO = new Bar; echo $obj_FOO->getName(); 'obj_FOO'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.