Initial commit
[mono.git] / mcs / class / referencesource / System / compmod / system / componentmodel / GuidConverter.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="GuidConverter.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 /*
8  */
9 namespace System.ComponentModel {
10     using Microsoft.Win32;
11     using System.ComponentModel.Design.Serialization;
12     using System.Diagnostics;
13     using System.Globalization;
14     using System.Reflection;
15     using System.Runtime.InteropServices;
16     using System.Runtime.Remoting;
17     using System.Runtime.Serialization.Formatters;
18     using System.Security.Permissions;    
19
20     /// <devdoc>
21     ///    <para>Provides a
22     ///       type converter to convert globally unique identifier objects to and from various
23     ///       other representations.</para>
24     /// </devdoc>
25     [HostProtection(SharedState = true)]
26     public class GuidConverter : TypeConverter {
27
28         /// <devdoc>
29         ///    <para>Gets a value indicating whether this
30         ///       converter can convert an object in the given source type to a globally unique identifier object
31         ///       using the context.</para>
32         /// </devdoc>
33         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
34             if (sourceType == typeof(string)) {
35                 return true;
36             }
37             return base.CanConvertFrom(context, sourceType);
38         }
39
40         /// <devdoc>
41         ///    <para>Gets a value indicating whether this converter can
42         ///       convert an object to the given destination type using the context.</para>
43         /// </devdoc>
44         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
45             if (destinationType == typeof(InstanceDescriptor)) {
46                 return true;
47             }
48             return base.CanConvertTo(context, destinationType);
49         }
50
51         /// <devdoc>
52         ///    <para>Converts
53         ///       the given object to a globally unique identifier object.</para>
54         /// </devdoc>
55         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
56             if (value is string) {
57                 string text = ((string)value).Trim();
58                 return new Guid(text);
59             }
60             return base.ConvertFrom(context, culture, value);
61         }
62         
63         /// <devdoc>
64         ///      Converts the given object to another type.  The most common types to convert
65         ///      are to and from a string object.  The default implementation will make a call
66         ///      to ToString on the object if the object is valid and if the destination
67         ///      type is string.  If this cannot convert to the desitnation type, this will
68         ///      throw a NotSupportedException.
69         /// </devdoc>
70         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
71             if (destinationType == null) {
72                 throw new ArgumentNullException("destinationType");
73             }
74
75             if (destinationType == typeof(InstanceDescriptor) && value is Guid) {
76                 ConstructorInfo ctor = typeof(Guid).GetConstructor(new Type[] {typeof(string)});
77                 if (ctor != null) {
78                     return new InstanceDescriptor(ctor, new object[] {value.ToString()});
79                 }
80             }
81             
82             return base.ConvertTo(context, culture, value, destinationType);
83         }
84     }
85 }
86