1 //------------------------------------------------------------------------------
2 // <copyright file="ConstraintConverter.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">[....]</owner>
6 // <owner current="true" primary="false">[....]</owner>
7 // <owner current="false" primary="false">[....]</owner>
8 //------------------------------------------------------------------------------
10 namespace System.Data {
12 using System.ComponentModel;
13 using System.ComponentModel.Design.Serialization;
14 using System.Globalization;
18 sealed internal class ConstraintConverter : ExpandableObjectConverter {
20 // converter classes should have public ctor
21 public ConstraintConverter() {
25 /// <para>Gets a value indicating whether this converter can
26 /// convert an object to the given destination type using the context.</para>
28 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
29 if (destinationType == typeof(InstanceDescriptor)) {
32 return base.CanConvertTo(context, destinationType);
36 /// Converts the given object to another type. The most common types to convert
37 /// are to and from a string object. The default implementation will make a call
38 /// to ToString on the object if the object is valid and if the destination
39 /// type is string. If this cannot convert to the desitnation type, this will
40 /// throw a NotSupportedException.
42 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
43 if (destinationType == null) {
44 throw new ArgumentNullException("destinationType");
47 if (destinationType == typeof(InstanceDescriptor) && value is Constraint) {
48 if (value is UniqueConstraint) {
49 UniqueConstraint constr = (UniqueConstraint)value;
50 System.Reflection.ConstructorInfo ctor = typeof(UniqueConstraint).GetConstructor(new Type[] { typeof(string), typeof(string[]), typeof(bool) } );
52 return new InstanceDescriptor(ctor, new object[] { constr.ConstraintName, constr.ColumnNames, constr.IsPrimaryKey });
55 ForeignKeyConstraint constr = (ForeignKeyConstraint)value;
56 System.Reflection.ConstructorInfo ctor = typeof(ForeignKeyConstraint).GetConstructor(new Type[] { typeof(string), typeof(string), typeof(string[]),
57 typeof(string[]), typeof(AcceptRejectRule), typeof(Rule), typeof(Rule) } );
59 return new InstanceDescriptor(ctor, new object[] { constr.ConstraintName, constr.ParentKey.Table.TableName, constr.ParentColumnNames,
60 constr.ChildColumnNames, constr.AcceptRejectRule, constr.DeleteRule, constr.UpdateRule });
64 return base.ConvertTo(context, culture, value, destinationType);