467,169 Members | 986 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

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
  • viewed: 1632
Share:
4 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Edvard Majakari | last post: by
reply views Thread by Brett Selleck | last post: by
15 posts views Thread by Carlos Lozano | last post: by
1 post views Thread by Senthil | last post: by
15 posts views Thread by David Thielen | last post: by
17 posts views Thread by Lee Harr | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.