Search:
Namespaces
Discussions
.NET v1.1
Feedback
How to create class instance automatically in C#
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.csharp
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Steven.Xu (VIP)
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.
Reply to this message...
Nick Malik
Take a look at the Reflection classes. You can create objects by passing
the name of their type.
--- Nick
"Steven.Xu" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Steven.Xu (VIP)
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
Reply to this message...
Steven.Xu (VIP)
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
Reply to this message...
Daniel O'Connell [C# MVP] (VIP)
"Steven.Xu" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Try
CClass01 c = CreateObjInstance(typeof(namespace1.namespace11.CClass01),1,2);
The typeof operator returns the type for any given class name, including
namespaces.
> Steven
Reply to this message...
Daniel O'Connell [C# MVP] (VIP)
"Daniel O'Connell [C# MVP]" <
Click here to reveal e-mail address
> wrote in
message news:
Click here to reveal e-mail address
...
[Original message clipped]
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.
[Original message clipped]
Reply to this message...
Steven.Xu (VIP)
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
Reply to this message...
Steven.Xu (VIP)
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
Reply to this message...
System.Type
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com