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. 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.
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
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
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
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
"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
"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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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()...
|
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...
|
by: mlt |
last post by:
I have the following class:
class Test {
public:
Test() {
a = 33;
}
private:
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |