ITypeDescriptorContext Interface

Provides contextual information about a component, such as its container and property descriptor.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

[ComVisibleAttribute(true)]
public interface ITypeDescriptorContext : IServiceProvider

The ITypeDescriptorContext type exposes the following members.

  NameDescription
Public propertyContainerGets the container representing this TypeDescriptor request.
Public propertyInstanceGets the object that is connected with this type descriptor request.
Public propertyPropertyDescriptorGets the PropertyDescriptor that is associated with the given context item.
Top

  NameDescription
Public methodGetServiceGets the service object of the specified type. (Inherited from IServiceProvider.)
Public methodOnComponentChangedRaises the ComponentChanged event.
Public methodOnComponentChangingRaises the ComponentChanging event.
Top

The ITypeDescriptorContext interface provides contextual information about a component. ITypeDescriptorContext is typically used at design time to provide information about a design-time container. This interface is commonly used in type conversion. For details, see TypeConverter.

NoteNote

Do not rely on the presence of this interface when you design a type converter. If a particular interface, property, or service is necessary but not available, the type converter should return null rather than throw an exception. This interface's properties can return null at any time, and you should plan for this.

The following code example demonstrates how to use the ITypeDescriptorContext interface to support type conversion.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft