cast of arrays with a given type

A

am72de

Hi all,

I have to write an editor for Visual Studio. It's one editor for
different kind of Arrays: Button[], TextBox[] and so on.
The intern array is Control[], so I can use it for all of the
inherited controls.

But now I don't know how to convert a Control[] back to the given
type.

Please see the following example:

Button[] btns = new Button[30];
Control[] ctls = (Control[])btns;

The obvious way to cast the ctls back would be:

Button[] btns2 = (Button[])ctls;

Does anybody know how to convert it back with only a given type?

Something like that: Button[] btns3 = ctls as typeof(Button[]);

Thanks in advance
Andy
 
M

Martin Bonner

Hi all,

I have to write an editor for Visual Studio. It's one editor for
different kind of Arrays: Button[], TextBox[] and so on.
The intern array is Control[], so I can use it for all of the
inherited controls.

But now I don't know how to convert a Control[] back to the given
type.

Why do you want to? Can you not just convert the element you /
extract/ from the array?
Please see the following example:

Button[] btns = new Button[30];
Control[] ctls = (Control[])btns;

The obvious way to cast the ctls back would be:

Button[] btns2 = (Button[])ctls;

.... so instead of doing that, you would do:

Button b = (Button)(ctls);
Does anybody know how to convert it back with only a given type?

Something like that: Button[] btns3 = ctls as typeof(Button[]);

I think what you mean, is that you have a function which has an
argument of type System.Type, and you want to use that to correctly
cast something. Basically, I don't think you can, because there is no
way to declare the argument.

You have a number of choices:
1. Just stick to the properties that are common to "Control".

2. If the range of types is small, do a huge great switch, and handle
each one seperately (you might be able to make the body of the switch
be a function call, and generate the different functions via
generics).

3. Do /everything/ via reflection (get properties, set properties,
call methods). (That is going to be hard work).

4. Generate the code at runtime, and compile it into a subsiduary
assembly.
 
A

am72de

I have to write an editor for Visual Studio. It's one editor for
different kind of Arrays: Button[], TextBox[] and so on.
The intern array is Control[], so I can use it for all of the
inherited controls.
But now I don't know how to convert a Control[] back to the given
type.

Why do you want to?  Can you not just convert the element you /
extract/ from the array?


Please see the following example:
        Button[] btns = new Button[30];
        Control[] ctls = (Control[])btns;
The obvious way to cast the ctls back would be:
        Button[] btns2 = (Button[])ctls;

... so instead of doing that, you would do:

          Button b = (Button)(ctls);


Does anybody know how to convert it back with only a given type?
Something like that:    Button[] btns3 = ctls as typeof(Button[]);

I think what you mean, is that you have a function which has an
argument of type System.Type, and you want to use that to correctly
cast something.  Basically, I don't think you can, because there is no
way to declare the argument.

You have a number of choices:
1.  Just stick to the properties that are common to "Control".

2.  If the range of types is small, do a huge great switch, and handle
each one seperately (you might be able to make the body of the switch
be a function call, and generate the different functions via
generics).

3.  Do /everything/ via reflection (get properties, set properties,
call methods).  (That is going to be hard work).

4.  Generate the code at runtime, and compile it into a subsiduary
assembly.


Unfortunately the editor should handle as many Controls, Buttons and
even Interfaces as possible. All works fine, except the automatic back
cast via a type.
Because the UITypeEditor.EditValue gives me only the
Propertydescriptor - and I don't want to cast every single type - I
thought there has to be a better and simpler way.
 
B

Ben Voigt [C++ MVP]

Unfortunately the editor should handle as many Controls, Buttons and
even Interfaces as possible. All works fine, except the automatic back
cast via a type.
Because the UITypeEditor.EditValue gives me only the
Propertydescriptor - and I don't want to cast every single type - I
thought there has to be a better and simpler way.

You need to use the PropertyDescriptor.SetValue and friends, there shouldn't
be any casting of the control type required (the component parameter is type
System::Object).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top