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

Creating classes by name

Hi,

I'm new to .net so please forgive me if I've been stupid.

I've got this -

BaseCommand obj = null;
String convClassName;
....
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null,convClassName);
obj = (BaseCommand)handle.Unwrap();

That'll create me an instance of the class called convClassName with
the default constructor.

I now want to use a non-default constructor. Looking at the versions
of CreateInstance, if I pick one that has a list of arguments, I also
have to provide something called an "activation attribute" and
"security info". But I've no idea how to fill these in! I made a
start -

System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null, convClassName,
false, // case sensitive
0, // bindingAttr
null, // binder
args,
null, // culture
activationAtr,
security
);

"activationAtr" has to have one or more items. What do I put in it?

Thanks.

Peter.

Aug 1 '07 #1
4 1784
Peter,

Why not get an instance of Type that represents the Type that you are
trying to create, and then use the overload that takes an instance of Type
and an array of objects (representing the parameters to pass to the
constructor)?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Smithson" <Pe************@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
Hi,

I'm new to .net so please forgive me if I've been stupid.

I've got this -

BaseCommand obj = null;
String convClassName;
...
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null,convClassName);
obj = (BaseCommand)handle.Unwrap();

That'll create me an instance of the class called convClassName with
the default constructor.

I now want to use a non-default constructor. Looking at the versions
of CreateInstance, if I pick one that has a list of arguments, I also
have to provide something called an "activation attribute" and
"security info". But I've no idea how to fill these in! I made a
start -

System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null, convClassName,
false, // case sensitive
0, // bindingAttr
null, // binder
args,
null, // culture
activationAtr,
security
);

"activationAtr" has to have one or more items. What do I put in it?

Thanks.

Peter.

Aug 1 '07 #2
Hi,

Short answer - because I don't know how to.

I did look at the constructor for "Type" but it had no parameters so
I'm not sure how I'd create it.

Can you give me a pointer?

Thanks.

Peter.

On Aug 1, 4:04 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Peter,

Why not get an instance of Type that represents the Type that you are
trying to create, and then use the overload that takes an instance of Type
and an array of objects (representing the parameters to pass to the
constructor)?

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Peter Smithson" <Peter_Smith...@hotmail.comwrote in message

news:11**********************@19g2000hsx.googlegro ups.com...
Hi,
I'm new to .net so please forgive me if I've been stupid.
I've got this -
BaseCommand obj = null;
String convClassName;
...
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null,convClassName);
obj = (BaseCommand)handle.Unwrap();
That'll create me an instance of the class called convClassName with
the default constructor.
I now want to use a non-default constructor. Looking at the versions
of CreateInstance, if I pick one that has a list of arguments, I also
have to provide something called an "activation attribute" and
"security info". But I've no idea how to fill these in! I made a
start -
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null, convClassName,
false, // case sensitive
0, // bindingAttr
null, // binder
args,
null, // culture
activationAtr,
security
);
"activationAtr" has to have one or more items. What do I put in it?
Thanks.
Peter.- Hide quoted text -

- Show quoted text -

Aug 1 '07 #3
Peter,

You don't use a constructor to create a Type instance, you can call the
static GetType method on the Type class and it will give you an instance of
Type that you can use.

i
"Peter Smithson" <Pe************@hotmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
Hi,

Short answer - because I don't know how to.

I did look at the constructor for "Type" but it had no parameters so
I'm not sure how I'd create it.

Can you give me a pointer?

Thanks.

Peter.

On Aug 1, 4:04 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>Peter,

Why not get an instance of Type that represents the Type that you are
trying to create, and then use the overload that takes an instance of
Type
and an array of objects (representing the parameters to pass to the
constructor)?

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Peter Smithson" <Peter_Smith...@hotmail.comwrote in message

news:11**********************@19g2000hsx.googlegr oups.com...
Hi,
I'm new to .net so please forgive me if I've been stupid.
I've got this -
BaseCommand obj = null;
String convClassName;
...
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null,convClassName);
obj = (BaseCommand)handle.Unwrap();
That'll create me an instance of the class called convClassName with
the default constructor.
I now want to use a non-default constructor. Looking at the versions
of CreateInstance, if I pick one that has a list of arguments, I also
have to provide something called an "activation attribute" and
"security info". But I've no idea how to fill these in! I made a
start -
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null, convClassName,
false, // case sensitive
0, // bindingAttr
null, // binder
args,
null, // culture
activationAtr,
security
);
"activationAtr" has to have one or more items. What do I put in it?
Thanks.
Peter.- Hide quoted text -

- Show quoted text -


Aug 1 '07 #4
Hi,

I see - easy once you have the info.

This is what I do now -

Object [] args = new Object[1];
args[0] = (String)"hello";
Type type = Type.GetType("our namespace here" + convClassName,
true);
obj = (BaseCommand)Activator.CreateInstance(type, args);

This calls the constructor which has one string parameter.

Thanks for your help.

Peter.

On Aug 1, 6:16 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Peter,

You don't use a constructor to create a Type instance, you can call the
static GetType method on the Type class and it will give you an instance of
Type that you can use.

i"Peter Smithson" <Peter_Smith...@hotmail.comwrote in message

news:11*********************@k79g2000hse.googlegro ups.com...
Hi,
Short answer - because I don't know how to.
I did look at the constructor for "Type" but it had no parameters so
I'm not sure how I'd create it.
Can you give me a pointer?
Thanks.
Peter.
On Aug 1, 4:04 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Peter,
Why not get an instance of Type that represents the Type that you are
trying to create, and then use the overload that takes an instance of
Type
and an array of objects (representing the parameters to pass to the
constructor)?
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"Peter Smithson" <Peter_Smith...@hotmail.comwrote in message
>news:11**********************@19g2000hsx.googlegr oups.com...
Hi,
I'm new to .net so please forgive me if I've been stupid.
I've got this -
BaseCommand obj = null;
String convClassName;
...
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null,convClassName);
obj = (BaseCommand)handle.Unwrap();
That'll create me an instance of the class called convClassName with
the default constructor.
I now want to use a non-default constructor. Looking at the versions
of CreateInstance, if I pick one that has a list of arguments, I also
have to provide something called an "activation attribute" and
"security info". But I've no idea how to fill these in! I made a
start -
System.Runtime.Remoting.ObjectHandle handle =
Activator.CreateInstance(null, convClassName,
false, // case sensitive
0, // bindingAttr
null, // binder
args,
null, // culture
activationAtr,
security
);
"activationAtr" has to have one or more items. What do I put in it?
Thanks.
Peter.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -

Aug 2 '07 #5

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

Similar topics

4
by: Edvard Majakari | last post by:
Greetings, fellow Pythonistas! I'm about to create three modules. As an avid TDD fan I'd like to create typical 'use-cases' for each of these modules. One of them is rather large, and I wondered...
0
by: Brett Selleck | last post by:
We have an issue where the JAXB generated classes are creating an interface which references itself. The Schema is valid, and I have not seen this ran into before. The code is below. What is...
4
by: Bill | last post by:
I would like to create a static array of classes (or structs) to be used in populating name/value pairs in various WebForm drop down list boxes, but am not quite sure of the construct (or rather to...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
1
by: Senthil | last post by:
Con is the file name for a reserved device name(i think it is for console). So you cannot create a file with name 'con'. choose some other name senthil >-----Original Message----- >Hi...
15
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. ...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
2
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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...

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.