Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / RelationshipConverter.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="RelationshipConverter.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</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     sealed internal class RelationshipConverter : ExpandableObjectConverter {
17
18         // converter classes should have public ctor
19         public RelationshipConverter() {
20         }
21
22
23         /// <devdoc>
24         ///    <para>Gets a value indicating whether this converter can
25         ///       convert an object to the given destination type using the context.</para>
26         /// </devdoc>
27         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
28             if (destinationType == typeof(InstanceDescriptor)) {
29                 return true;
30             }
31             return base.CanConvertTo(context, destinationType);
32         }
33
34         /// <devdoc>
35         ///      Converts the given object to another type.  The most common types to convert
36         ///      are to and from a string object.  The default implementation will make a call
37         ///      to ToString on the object if the object is valid and if the destination
38         ///      type is string.  If this cannot convert to the desitnation type, this will
39         ///      throw a NotSupportedException.
40         /// </devdoc>
41         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
42             if (destinationType == null) {
43                 throw new ArgumentNullException("destinationType");
44             }
45
46             System.Reflection.ConstructorInfo ctor = null;
47             object[] values = null;
48
49             if (destinationType == typeof(InstanceDescriptor) && value is DataRelation) {
50                 DataRelation rel = (DataRelation) value;
51                 DataTable parentTable = rel.ParentKey.Table;
52                 DataTable childTable = rel.ChildKey.Table;
53
54                 if (System.Data.Common.ADP.IsEmpty(parentTable.Namespace) && System.Data.Common.ADP.IsEmpty(childTable.Namespace)) {
55                     ctor = typeof(DataRelation).GetConstructor(new Type[] { typeof(string) /*relationName*/, typeof(string) /*parentTableName*/, typeof(string) /*childTableName */, 
56                         typeof(string[]) /*parentColumnNames */, typeof(string[])  /*childColumnNames*/, typeof(bool) /*nested*/ } );
57                     
58                     values = new object[] { rel.RelationName, rel.ParentKey.Table.TableName, rel.ChildKey.Table.TableName,rel.ParentColumnNames, rel.ChildColumnNames, rel.Nested };
59                 }
60                 else {
61                     ctor = typeof(DataRelation).GetConstructor(new Type[] { typeof(string)/*relationName*/, typeof(string)/*parentTableName*/, typeof(string)/*parentTableNamespace*/,
62                         typeof(string)/*childTableName */, typeof(string)/*childTableNamespace */, 
63                         typeof(string[])/*parentColumnNames */, typeof(string[]) /*childColumnNames*/, typeof(bool) /*nested*/} );
64
65                     values = new object[] { rel.RelationName, rel.ParentKey.Table.TableName, rel.ParentKey.Table.Namespace, rel.ChildKey.Table.TableName, 
66                         rel.ChildKey.Table.Namespace, rel.ParentColumnNames, rel.ChildColumnNames, rel.Nested };
67                 }
68              
69                 return new InstanceDescriptor(ctor, values);
70             }
71             
72             return base.ConvertTo(context, culture, value, destinationType);
73         }
74     }
75 }
76