Lire en anglais

Partager via


ITypeDescriptorContext Interface

Définition

Fournit des informations contextuelles sur un composant telles que son conteneur et son descripteur de propriété.

C#
public interface ITypeDescriptorContext : IServiceProvider
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface ITypeDescriptorContext : IServiceProvider
Dérivé
Attributs
Implémente

Exemples

L’exemple de code suivant montre comment utiliser l’interface pour prendre en charge la ITypeDescriptorContext conversion de type.

C#
namespace Microsoft.Samples.InstanceDescriptorSample
{
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design.Serialization;
    using System.Drawing;
    using System.Globalization;
    using System.Reflection;

    //  This sample shows how to support code generation for a custom type 
    //  of object using a type converter and InstanceDescriptor objects.

    //  To use this code, copy it to a file and add the file to a project. 
    //  Then add a component to the project and declare a Triangle field and 
    //  a public property with accessors for the Triangle field on the component.

    //  The Triangle property will be persisted using code generation.

    [TypeConverter(typeof(Triangle.TriangleConverter))]
    public class Triangle
    {
        // Triangle members.
        Point P1;
        Point P2;
        Point P3;

        public Point Point1 {
            get {
                return P1;
            }
            set {
                P1 = value;
            }
        }
        public Point Point2 {
            get 
            {
                return P2;
            }
            set 
            {
                P2 = value;
            }
        }
        public Point Point3 {
            get 
            {
                return P3;
            }
            set 
            {
                P3 = value;
            }
        }

        public Triangle(Point point1,Point point2,Point point3) {
            P1 = point1;
            P2 = point2;
            P3 = point3;
        }

        // A TypeConverter for the Triangle object.  Note that you can make it internal,
        //  private, or any scope you want and the designers will still be able to use
        //  it through the TypeDescriptor object.  This type converter provides the
        //  capability to convert to an InstanceDescriptor.  This object can be used by 
    //  the .NET Framework to generate source code that creates an instance of a 
    //  Triangle object.
        internal class TriangleConverter : TypeConverter
        {
            // This method overrides CanConvertTo from TypeConverter. This is called when someone
            //  wants to convert an instance of Triangle to another type.  Here,
            //  only conversion to an InstanceDescriptor is supported.
            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                if (destinationType == typeof(InstanceDescriptor))
                {
                    return true;
                }

                // Always call the base to see if it can perform the conversion.
                return base.CanConvertTo(context, destinationType);
            }

            // This code performs the actual conversion from a Triangle to an InstanceDescriptor.
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (destinationType == typeof(InstanceDescriptor))
                {
                    ConstructorInfo ci = typeof(Triangle).GetConstructor(new Type[]{typeof(Point),
                                                    typeof(Point),typeof(Point)});
                    Triangle t = (Triangle) value;
                    return new InstanceDescriptor(ci,new object[]{t.Point1,t.Point2,t.Point3});
                }

                // Always call base, even if you can't convert.
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }
    }

    public class TestComponent : System.ComponentModel.Component 
    {
        Triangle myTriangle;

        public TestComponent() {
            myTriangle = new Triangle(
                new Point(5,5),
                new Point(10,10),
                new Point(1,8)
                );
        }

        public Triangle MyTriangle {
            get {
                return myTriangle;
            }
            set {
                myTriangle = value;
            }
        }
    }
}

Remarques

L’interface ITypeDescriptorContext fournit des informations contextuelles sur un composant. ITypeDescriptorContext est généralement utilisé au moment de la conception pour fournir des informations sur un conteneur au moment de la conception. Cette interface est couramment utilisée dans la conversion de type. Pour plus d'informations, consultez TypeConverter.

Note

Ne vous fiez pas à la présence de cette interface lorsque vous concevez un convertisseur de type. Si une interface, une propriété ou un service particulier est nécessaire mais non disponible, le convertisseur de type doit retourner null au lieu de lever une exception. Les propriétés de cette interface peuvent retourner null à tout moment, et vous devez planifier cela.

Propriétés

Container

Obtient le conteneur représentant cette demande TypeDescriptor.

Instance

Obtient l'objet qui est connecté avec cette demande de descripteur de type.

PropertyDescriptor

Obtient PropertyDescriptor qui est associé à l'élément de contexte donné.

Méthodes

GetService(Type)

Obtient l'objet service du type spécifié.

(Hérité de IServiceProvider)
OnComponentChanged()

Déclenche l’événement ComponentChanged.

OnComponentChanging()

Déclenche l’événement ComponentChanging.

Méthodes d’extension

GetKeyedService<T>(IServiceProvider, Object)

Obtient un service de type T à partir de .IServiceProvider

GetKeyedServices(IServiceProvider, Type, Object)

Obtient une énumération des services de type serviceType à partir de IServiceProvider.

GetKeyedServices<T>(IServiceProvider, Object)

Obtient une énumération des services de type T à partir de IServiceProvider.

GetRequiredKeyedService(IServiceProvider, Type, Object)

Obtient un service de type serviceType à partir de .IServiceProvider

GetRequiredKeyedService<T>(IServiceProvider, Object)

Obtient un service de type T à partir de .IServiceProvider

CreateAsyncScope(IServiceProvider)

Crée un AsyncServiceScope qui peut être utilisé pour résoudre des services délimités.

CreateScope(IServiceProvider)

Crée un IServiceScope qui peut être utilisé pour résoudre des services délimités.

GetRequiredService(IServiceProvider, Type)

Obtient le service de type serviceType à partir du IServiceProvider.

GetRequiredService<T>(IServiceProvider)

Obtient le service de type T à partir du IServiceProvider.

GetService<T>(IServiceProvider)

Obtient le service de type T à partir du IServiceProvider.

GetServices(IServiceProvider, Type)

Obtient une énumération des services de type serviceType à partir du IServiceProvider.

GetServices<T>(IServiceProvider)

Obtient une énumération des services de type T à partir du IServiceProvider.

GetFakeLogCollector(IServiceProvider)

Obtient l’objet qui collecte les enregistrements de journal envoyés au faux journal.

GetFakeRedactionCollector(IServiceProvider)

Obtient le faux collecteur de redacteur instance à partir du conteneur d’injection de dépendances.

S’applique à

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Voir aussi