On Mar 12, 4:21 pm, am7...@yahoo.de wrote:
> Hi all,
>
[quoted text clipped - 5 lines]
> 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:
>
[quoted text clipped - 4 lines]
>
> Button[] btns2 = (Button[])ctls;
... so instead of doing that, you would do:
Button b = (Button)(ctls[i]);
> 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.
am72de@yahoo.de - 12 Mar 2008 20:40 GMT
> On Mar 12, 4:21 pm, am7...@yahoo.de wrote:
>
[quoted text clipped - 46 lines]
> 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.
Ben Voigt [C++ MVP] - 12 Mar 2008 22:25 GMT
> 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).