Invalid port from UserControl to custom control

I have several controls that all inherit from UserControl, and I have a method MergeSortthat works in types UserControl(so that I cannot write 6 different methods), but when I return to my usual controls after sorting, it raises an error that am i doing wrong? The mergesort method requires a property Leftthat every user control uses, so how can I do this?

The MergeSort method looks like this:

public UserControl[] MergeSort(UserControl[] array)

Casting is as follows:

(CustomControl[])MergeSort(customControlArray);
+3
source share
2 answers

I think you need to drop each one manually:

CustomControl[] customControlArray;
...
UserControl[] sortedControls = MergeSort(customControlArray);
CustomControl[] sortedCustomControls = Array.ConvertAll<UserControl, CustomControl>(sortedControls, delegate(UserControl control)
{
    return (CustomControl)control;
});

.NET 3.5+ , "cooler".:)

: :

UserControl[] sortedControls = customControlArray.ToList().ConvertAll(c => (CustomControl)c).ToArray();

: -)

+2

( , , .)

, UserControl, . , , , , , ..

  • . , MyCustomControl ( , ), . UserControl, . / .
  • . ICustomControl ( , ), . UserControl, ICustomControl. / .

. , , - , . ( ) ( ). , , :)

+2

Source: https://habr.com/ru/post/1795061/


All Articles