Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data / System / Data / ConstraintConverter.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="ConstraintConverter.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">[....]</owner>
6 // <owner current="true" primary="false">[....]</owner>
7 // <owner current="false" primary="false">[....]</owner>
8 //------------------------------------------------------------------------------
9
10 namespace System.Data {
11     using System;
12     using System.ComponentModel;
13     using System.ComponentModel.Design.Serialization;
14     using System.Globalization;
15
16     /// <devdoc>
17     /// </devdoc>
18     sealed internal class ConstraintConverter : ExpandableObjectConverter {
19
20         // converter classes should have public ctor
21         public ConstraintConverter() {
22         }
23
24         /// <devdoc>
25         ///    <para>Gets a value indicating whether this converter can
26         ///       convert an object to the given destination type using the context.</para>
27         /// </devdoc>
28         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
29             if (destinationType == typeof(InstanceDescriptor)) {
30                 return true;
31             }
32             return base.CanConvertTo(context, destinationType);
33         }
34
35         /// <devdoc>
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.
41         /// </devdoc>
42         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
43             if (destinationType == null) {
44                 throw new ArgumentNullException("destinationType");
45             }
46
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) } );
51                     if (ctor != null)
52                         return new InstanceDescriptor(ctor, new object[] { constr.ConstraintName, constr.ColumnNames, constr.IsPrimaryKey });
53                 }
54                 else {
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) } );
58                     if (ctor != null)
59                         return new InstanceDescriptor(ctor, new object[] { constr.ConstraintName, constr.ParentKey.Table.TableName, constr.ParentColumnNames,
60                         constr.ChildColumnNames, constr.AcceptRejectRule, constr.DeleteRule, constr.UpdateRule });
61                 }                                        
62             }
63             
64             return base.ConvertTo(context, culture, value, destinationType);
65         }
66     }
67 }