Leggi in inglese

Condividi tramite


ITypeDescriptorContext Interfaccia

Definizione

Fornisce informazioni contestuali su un componente, ad esempio il relativo contenitore e il descrittore di proprietà.

C#
public interface ITypeDescriptorContext : IServiceProvider
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface ITypeDescriptorContext : IServiceProvider
Derivato
Attributi
Implementazioni

Esempio

Nell'esempio di codice seguente viene illustrato come usare l'interfaccia ITypeDescriptorContext per supportare la conversione dei tipi.

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;
            }
        }
    }
}

Commenti

L'interfaccia ITypeDescriptorContext fornisce informazioni contestuali su un componente. ITypeDescriptorContext viene in genere usato in fase di progettazione per fornire informazioni su un contenitore in fase di progettazione. Questa interfaccia viene comunemente usata nella conversione dei tipi. Per informazioni dettagliate, vedere TypeConverter.

Nota

Non basarsi sulla presenza di questa interfaccia quando si progetta un convertitore di tipi. Se una particolare interfaccia, proprietà o servizio è necessaria ma non disponibile, il convertitore di tipi deve restituire null anziché generare un'eccezione. Le proprietà di questa interfaccia possono restituire null in qualsiasi momento ed è consigliabile pianificare questa operazione.

Proprietà

Container

Ottiene il contenitore che rappresenta questa richiesta di TypeDescriptor.

Instance

Ottiene l'oggetto connesso a questa richiesta del descrittore di tipo.

PropertyDescriptor

Ottiene il PropertyDescriptor associato all'elemento di contesto specificato.

Metodi

GetService(Type)

Ottiene l'oggetto servizio del tipo specificato.

(Ereditato da IServiceProvider)
OnComponentChanged()

Genera l'evento ComponentChanged.

OnComponentChanging()

Genera l'evento ComponentChanging.

Metodi di estensione

GetKeyedService<T>(IServiceProvider, Object)

Ottenere il servizio di tipo T dal IServiceProvider.

GetKeyedServices(IServiceProvider, Type, Object)

Ottenere un'enumerazione dei servizi di tipo serviceType dal IServiceProvider.

GetKeyedServices<T>(IServiceProvider, Object)

Ottenere un'enumerazione dei servizi di tipo T dal IServiceProvider.

GetRequiredKeyedService(IServiceProvider, Type, Object)

Ottenere il servizio di tipo serviceType dal IServiceProvider.

GetRequiredKeyedService<T>(IServiceProvider, Object)

Ottenere il servizio di tipo T dal IServiceProvider.

CreateAsyncScope(IServiceProvider)

Crea un nuovo AsyncServiceScope che può essere usato per risolvere i servizi con ambito.

CreateScope(IServiceProvider)

Crea un nuovo IServiceScope che può essere usato per risolvere i servizi con ambito.

GetRequiredService(IServiceProvider, Type)

Ottenere il servizio di tipo serviceType dal IServiceProvider.

GetRequiredService<T>(IServiceProvider)

Ottenere il servizio di tipo T dal IServiceProvider.

GetService<T>(IServiceProvider)

Ottenere il servizio di tipo T dal IServiceProvider.

GetServices(IServiceProvider, Type)

Ottenere un'enumerazione dei servizi di tipo serviceType dal IServiceProvider.

GetServices<T>(IServiceProvider)

Ottenere un'enumerazione dei servizi di tipo T dal IServiceProvider.

GetFakeLogCollector(IServiceProvider)

Ottiene l'oggetto che raccoglie i record di log inviati al logger falso.

GetFakeRedactionCollector(IServiceProvider)

Ottiene l'istanza dell'agente di raccolta redactor fittizio dal contenitore di inserimento delle dipendenze.

Si applica a

Prodotto Versioni
.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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0, 2.1

Vedi anche