Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

InstanceDescriptor Class

Provides the information necessary to create an instance of an object. This class cannot be inherited.

Namespace: System.ComponentModel.Design.Serialization
Assembly: System (in system.dll)

public sealed class InstanceDescriptor
public final class InstanceDescriptor
public final class InstanceDescriptor
Not applicable.

InstanceDescriptor can store information that describes an instance of an object. This information can be used to create an instance of the object.

Some custom serializers use InstanceDescriptor to represent serializable objects. Several methods of a TypeDescriptor use InstanceDescriptor to represent or instantiate objects.

An InstanceDescriptor provides the following members:

  • A MemberInfo property that describes this object.

  • An Arguments property that consists of the constructor arguments that can be used to instantiate this object.

  • A Boolean IsComplete property that indicates whether the object is completely represented by the current information.

  • An Invoke method that can be used to create an instance of the represented object.

NoteNote:

The HostProtectionAttribute attribute applied to this class has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example illustrates how to use a type converter that participates in code generation through the use of instance descriptors.

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

package Microsoft.Samples.InstanceDescriptorSample; 

import System.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.Serialization.*;
import System.Drawing.*;
import System.Globalization.*;
import 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.
/** @ attribute TypeConverter(Triangle.TriangleConverter .class.ToType())
 */
public class Triangle
{
    // Triangle members.
    private Point P1;
    private Point P2;
    private Point P3;

    /** @property 
     */
    public Point get_Point1()
    {
        return P1;
    } //get_Point1

    /** @property 
     */
    public void set_Point1(Point value)
    {
        P1 = value;
    } //set_Point1

    /** @property 
     */
    public Point get_Point2()
    {
        return P2;
    } //get_Point2

    /** @property 
     */
    public void set_Point2(Point value)
    {
        P2 = value;
    } //set_Point2

    /** @property 
     */
    public Point get_Point3()
    {
        return P3;
    } //get_Point3

    /** @property 
     */
    public void set_Point3(Point value)
    {
        P3 = value;
    } //set_Point3

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

    // 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.
    class TriangleConverter extends 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 boolean CanConvertTo(ITypeDescriptorContext context,
            Type destinationType)
        {
            if (destinationType.Equals(InstanceDescriptor.class.ToType())) {
                return true;
            }
            // Always call the base to see if it can perform the conversion.
            return super.CanConvertTo(context, destinationType);
        } //CanConvertTo

        // This code performs the actual conversion from a Triangle to an 
        //InstanceDescriptor.
        public Object ConvertTo(ITypeDescriptorContext context,
            CultureInfo culture, Object value, Type destinationType)
        {
            if (destinationType.Equals(InstanceDescriptor.class.ToType())) {
                ConstructorInfo ci = Triangle.class.ToType().GetConstructor(
                    new Type[] { Point.class.ToType(), Point.class.ToType(),
                    Point.class.ToType() });
                Triangle t = ((Triangle)(value));
                return new InstanceDescriptor(ci, new Object[] { t.get_Point1(),
                    t.get_Point2(), t.get_Point3() });
            }
            // Always call base, even if you can't convert.
            return super.ConvertTo(context, culture, value, destinationType);
        } //ConvertTo
    } //TriangleConverter
} //Triangle

public class TestComponent extends System.ComponentModel.Component
{
    private Triangle myTriangle;

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

    /** @property 
     */
    public Triangle get_MyTriangle()
    {
        return myTriangle;
    } //get_MyTriangle

    /** @property 
     */
    public void set_MyTriangle(Triangle value)
    {
        myTriangle = value;
    } //set_MyTriangle
} //TestComponent

System.Object
  System.ComponentModel.Design.Serialization.InstanceDescriptor
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.