473,382 Members | 1,635 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,382 software developers and data experts.

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.namespace11.CClass01. I want to create it's instance in a
function named CreateObjInstance. 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 7887
Take a look at the Reflection classes. You can create objects by passing
the name of their type.

--- Nick

"Steven.Xu" <St******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
Hello everyone!
I have a problem about create an instance automatically in C#. Now I have a class: namespace1.namespace11.CClass01. I want to create it's instance in a function named CreateObjInstance. 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 CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 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 CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 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 CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 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 CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 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******@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 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 = CreateObjInstance(typeof(namespace1.namespace11.CC lass01),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**************@TK2MSFTNGP10.phx.gbl...

"Steven.Xu" <St******@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 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 =
CreateObjInstance(typeof(namespace1.namespace11.CC lass01),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
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...
0
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...
4
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...
7
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...
27
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...
2
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...
12
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()...
1
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...
6
by: mlt | last post by:
I have the following class: class Test { public: Test() { a = 33; } private:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.