Creating classes by name

  • Thread starter Thread starter Peter Smithson
  • Start date Start date
P

Peter Smithson

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.
 
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)?
 
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.

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]
- (e-mail address removed)




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?

Peter.- Hide quoted text -

- Show quoted text -
 
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 said:
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.

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]
- (e-mail address removed)




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?

Peter.- Hide quoted text -

- Show quoted text -
 
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.

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.




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?
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]
- (e-mail address removed)

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 -
 
Back
Top