2 // Copyright (C) 2010 Novell Inc. http://novell.com
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 using System.Collections.Generic;
26 namespace System.Xaml.Schema
28 public class XamlValueConverter<TConverterBase> : IEquatable<XamlValueConverter<TConverterBase>>
29 where TConverterBase : class
31 public XamlValueConverter (Type converterType, XamlType targetType)
32 : this (converterType, targetType, null)
36 public XamlValueConverter (Type converterType, XamlType targetType, string name)
38 if (converterType == null && targetType == null && name == null)
39 throw new ArgumentException ("Either of converterType, targetType or name must be non-null");
40 ConverterType = converterType;
41 TargetType = targetType;
45 TConverterBase converter_instance;
46 public TConverterBase ConverterInstance {
48 if (converter_instance == null)
49 converter_instance = CreateInstance ();
50 return converter_instance;
53 public Type ConverterType { get; private set; }
54 public string Name { get; private set; }
55 public XamlType TargetType { get; private set; }
58 public static bool operator == (XamlValueConverter<TConverterBase> left, XamlValueConverter<TConverterBase> right)
60 return IsNull (left) ? IsNull (right) : left.Equals (right);
63 static bool IsNull (XamlValueConverter<TConverterBase> a)
65 return Object.ReferenceEquals (a, null);
68 public static bool operator != (XamlValueConverter<TConverterBase> left, XamlValueConverter<TConverterBase> right)
70 return IsNull (left) ? !IsNull (right) : IsNull (right) || left.ConverterType != right.ConverterType || left.TargetType != right.TargetType || left.Name != right.Name;
73 public bool Equals (XamlValueConverter<TConverterBase> other)
75 return !IsNull (other) && ConverterType == other.ConverterType && TargetType == other.TargetType && Name == other.Name;
78 public override bool Equals (object obj)
80 var a = obj as XamlValueConverter<TConverterBase>;
84 protected virtual TConverterBase CreateInstance ()
86 if (ConverterType == null)
89 if (!typeof (TConverterBase).IsAssignableFrom (ConverterType))
90 throw new XamlSchemaException (String.Format ("ConverterType '{0}' is not derived from '{1}' type", ConverterType, typeof (TConverterBase)));
92 return (TConverterBase) Activator.CreateInstance (ConverterType);
95 public override int GetHashCode ()
97 var ret = ConverterType != null ? ConverterType.GetHashCode () : 0;
99 if (TargetType != null)
100 ret += TargetType.GetHashCode ();
103 ret += Name.GetHashCode ();
107 public override string ToString ()
111 if (ConverterType != null && TargetType != null)
112 return String.Concat (ConverterType.Name, "(", TargetType.Name, ")");
113 else if (ConverterType != null)
114 return ConverterType.Name;
116 return TargetType.Name;