ITypedList Interfaccia

Definizione

Fornisce le funzionalità per scoprire lo schema di un elenco associabile, in cui le proprietà disponibili per l'associazione differiscono dalle proprietà pubbliche dell'oggetto verso cui effettuare l'associazione.

public interface class ITypedList
public interface ITypedList
type ITypedList = interface
Public Interface ITypedList
Derivato

Esempio

Nell'esempio di codice seguente viene illustrato come implementare l'interfaccia ITypedList . Un tipo generico denominato SortableBindingList deriva dalla classe e implementa l'interfaccia BindingList<T>ITypedList . Per un elenco di codice completo, vedere Procedura: Implementare l'interfaccia ITypedList.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;

namespace ITypedListCS
{
    [Serializable()]
    public class SortableBindingList<T> : BindingList<T>, ITypedList
    {
        [NonSerialized()]
        private PropertyDescriptorCollection properties;

        public SortableBindingList() : base()
        {
            // Get the 'shape' of the list. 
            // Only get the public properties marked with Browsable = true.
            PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(
                typeof(T), 
                new Attribute[] { new BrowsableAttribute(true) });

            // Sort the properties.
            properties = pdc.Sort();
        }

        #region ITypedList Implementation

        public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
        {
            PropertyDescriptorCollection pdc;

            if (listAccessors!=null && listAccessors.Length>0)
            {
                // Return child list shape.
                pdc = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);
            }
            else
            {
                // Return properties in sort order.
                pdc = properties;
            }

            return pdc;
        }

        // This method is only used in the design-time framework 
        // and by the obsolete DataGrid control.
        public string GetListName(PropertyDescriptor[] listAccessors)
        {   
            return typeof(T).Name;
        }

        #endregion
    }
}
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Windows.Forms

<Serializable()> _
Public Class SortableBindingList(Of Tkey)
    Inherits BindingList(Of Tkey)
    Implements ITypedList

    <NonSerialized()> _
    Private properties As PropertyDescriptorCollection

    Public Sub New()
        MyBase.New()

        ' Get the 'shape' of the list. 
        ' Only get the public properties marked with Browsable = true.
        Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(Tkey), New Attribute() {New BrowsableAttribute(True)})

        ' Sort the properties.
        properties = pdc.Sort()

    End Sub

#Region "ITypedList Implementation"

    Public Function GetItemProperties(ByVal listAccessors() As System.ComponentModel.PropertyDescriptor) As System.ComponentModel.PropertyDescriptorCollection Implements System.ComponentModel.ITypedList.GetItemProperties

        Dim pdc As PropertyDescriptorCollection

        If (Not (listAccessors Is Nothing)) And (listAccessors.Length > 0) Then
            ' Return child list shape
            pdc = ListBindingHelper.GetListItemProperties(listAccessors(0).PropertyType)
        Else
            ' Return properties in sort order
            pdc = properties
        End If

        Return pdc

    End Function

    ' This method is only used in the design-time framework 
    ' and by the obsolete DataGrid control.
    Public Function GetListName( _
    ByVal listAccessors() As PropertyDescriptor) As String _
    Implements System.ComponentModel.ITypedList.GetListName

        Return GetType(Tkey).Name

    End Function

#End Region

End Class

Commenti

Usare questa interfaccia se, ad esempio, si usa un DataView oggetto che rappresenta una customer tabella, si vuole associare alle proprietà dell'oggetto rappresentatoDataView, non alle proprietà dell'oggetto rappresentato, non alle proprietà dell'oggetto customerDataView.

Questa interfaccia non è necessaria per il supporto in fase di progettazione di un elenco associabile.

L'associazione ai dati può verificarsi in fase di esecuzione o in una finestra di progettazione, ma esistono regole per entrambi. In fase di esecuzione è possibile associare ai dati in uno dei seguenti elementi:

  • Array

  • Implementer di IList, fornito che l'implementer ha una proprietà fortemente tipizzata Item[] , ovvero , è Type tutto ma Object. È possibile eseguire questa operazione eseguendo l'implementazione predefinita di Item[] private. Se si vuole creare un oggetto IList che segue le regole di una raccolta fortemente tipizzata, è consigliabile derivare da CollectionBase.

  • Implementer di ITypedList.

In una finestra di progettazione è possibile inizializzare l'associazione agli Component oggetti seguendo le stesse regole.

Per altre informazioni sull'associazione a un'origine dati, vedere la System.Windows.Forms.Binding classe .

Metodi

GetItemProperties(PropertyDescriptor[])

Restituisce l'oggetto PropertyDescriptorCollection che rappresenta le proprietà di ogni elemento usato per associare i dati.

GetListName(PropertyDescriptor[])

Restituisce il nome dell'elenco.

Si applica a

Vedi anche