473,804 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create class instance automatically in C#

Hello everyone!
I have a problem about create an instance automatically in C#. Now I have a
class: namespace1.name space11.CClass0 1. I want to create it's instance in a
function named CreateObjInstan ce. And the function not only generate just
CClass01. So I must sent the class type(whit it's namespace) into the
function.
There has two question following.
The first is I can't send a class type into function. It must is a object
instance. Can I do like that?
Secondly, How can I generate a class instance which not like: CClass01
newInstance = new CClass01. I need a CClass01 instance after I told the class
name to the function. Of course with some parameter of structure.

Thanks.
Nov 16 '05 #1
7 7901
Take a look at the Reflection classes. You can create objects by passing
the name of their type.

--- Nick

"Steven.Xu" <St******@discu ssions.microsof t.com> wrote in message
news:34******** *************** ***********@mic rosoft.com...
Hello everyone!
I have a problem about create an instance automatically in C#. Now I have a class: namespace1.name space11.CClass0 1. I want to create it's instance in a function named CreateObjInstan ce. And the function not only generate just
CClass01. So I must sent the class type(whit it's namespace) into the
function.
There has two question following.
The first is I can't send a class type into function. It must is a object
instance. Can I do like that?
Secondly, How can I generate a class instance which not like: CClass01
newInstance = new CClass01. I need a CClass01 instance after I told the class name to the function. Of course with some parameter of structure.

Thanks.

Nov 16 '05 #2
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstan ce(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstan ce(namespace1.n amespace11.CCla ss01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Nov 16 '05 #3
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstan ce(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstan ce(namespace1.n amespace11.CCla ss01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Nov 16 '05 #4
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstan ce(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstan ce(namespace1.n amespace11.CCla ss01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Nov 16 '05 #5
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstan ce(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstan ce(namespace1.n amespace11.CCla ss01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Nov 16 '05 #6

"Steven.Xu" <St******@discu ssions.microsof t.com> wrote in message
news:07******** *************** ***********@mic rosoft.com...
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstan ce(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstan ce(namespace1.n amespace11.CCla ss01, 1, 2)
As you know, the first parameter is error. because the namespace of class
is
not an instance of System.Type. But i need to send the class type like
this.
Do you know how to? Thanks!

Try
CClass01 c = CreateObjInstan ce(typeof(names pace1.namespace 11.CClass01),1, 2);

The typeof operator returns the type for any given class name, including
namespaces.

Steven

Nov 16 '05 #7

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OG******** ******@TK2MSFTN GP10.phx.gbl...

"Steven.Xu" <St******@discu ssions.microsof t.com> wrote in message
news:07******** *************** ***********@mic rosoft.com...
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstan ce(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstan ce(namespace1.n amespace11.CCla ss01, 1, 2)
As you know, the first parameter is error. because the namespace of class
is
not an instance of System.Type. But i need to send the class type like
this.
Do you know how to? Thanks!

Try
CClass01 c =
CreateObjInstan ce(typeof(names pace1.namespace 11.CClass01),1, 2);

The typeof operator returns the type for any given class name, including
namespaces.


Err, that is, a class *including* its namespace. A namespace on its own
doesn't have atype, it actually doesn't even exist as a seperate entity in
the runtime.
Steven


Nov 16 '05 #8

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

Similar topics

2
21469
by: Ed Sutton | last post by:
How can a WndProc be created inside a component? Currently I have a WndProc in my frmMain. It looks for WM_DEVICECHANGE messages for connection and removal events for a USB/Serial device. My frmMain uses a component to communicate with a USB/Serial device. I would prefer that this component handle the WM_DEVICECHANGE messages internally and only fires events to my frmMain when my USB device connects or disconnects. In case anyone...
0
3650
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should automatically pull up details on that record in the various text boxes and other controls on the right...
4
1650
by: Joe HM | last post by:
Hello - I have a Base Class where I want a New() implemented that can be called from the outside. This New() should create an instance of the appropriate cDerivedX Class ... The following shows a scenario where it was determined that cDerivedA is needed based on a configuration file. Dim lInstance As New cBase()
7
2022
by: jwang | last post by:
I am trying to instantiate a COM component in C# via late binding. The component appears to be launched, then it automatically gets unloaded. That is, the instance is unloaded after the CreateInstance returns. Any idea of what could be the problem? Thanks a lot. Here is the code segment I am using: Object target;
27
3802
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB updates the same data in all other four tables in the right places. I know it would be possible by using the ForeignKeyConstraint object. I have created the tables using the DataSet Visual Tool and I know it doesn't create any ForeignKeyConstraint obj....
2
12853
by: ashishjainism | last post by:
I am unable to install my windows service and the installutil is throwing the following error: Running a transacted installation. Beginning the Install phase of the installation. See the contents of the log file for the d:\practice in .net\mywindowsservice\mywindowsservice\mywindowsservice\bin\practiceservice.exe assembly's progress. The file is located at d:\practice in...
12
4104
by: Andy Terrel | last post by:
Okay does anyone know how to decorate class member functions? The following code gives me an error: Traceback (most recent call last): File "decorators2.py", line 33, in <module> s.update() File "decorators2.py", line 13, in __call__ retval = self.fn.__call__(*args,**kws) TypeError: update() takes exactly 1 argument (0 given)
1
3932
vinci
by: vinci | last post by:
Help Me!!!! T_T I want to create an instance of System.Windows.Forms.KeyEventArgs in vb.net for me to be able to use the .keyCode method of that class...is this possible??? basically, i want to create an instance of it in my txtbox_textchange event this is for monitoring the input of the user, if the user typed in a Space (keyCode 32) then it will not permit the user to type in again.. basically thats how i want it to be... for u guys...
6
2303
by: mlt | last post by:
I have the following class: class Test { public: Test() { a = 33; } private:
0
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.