.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
Converter.cs
Go to the documentation of this file.
1 // <copyright file=mitlicense.md url=http://lsauer.mit-license.org/ >
2 // Lo Sauer, 2013-2016
3 // </copyright>
4 // <summary> A tested, generic, portable, runtime-extensible type converter library </summary
5 // <language> C# > 6.0 </language>
6 // <version> 3.1.0.2 </version>
7 // <author> Lorenz Lo Sauer; people credited in the sources </author>
8 // <project> https://github.com/lsauer/dotnet-portable-type-cast </project>
9 namespace Core.TypeCast.Base
10 {
11  using System;
12  using System.Diagnostics;
13  using System.Reflection;
14  using System.Runtime.Serialization;
15 
20  [DebuggerDisplay("{DebuggerDisplay,nq}")]
21  [DataContract(IsReference = false, Name = "BaseConverter", Namespace = nameof(Base))]
22  public abstract class Converter : IConverter
23  {
29  private ConverterAttribute attribute;
30 
39  protected Converter(Type from, Type to, Type argument = null, Type baseType = null, ConverterAttribute attribute = null)
40  {
41  this.From = from?.GetTypeInfo();
42  this.To = to?.GetTypeInfo();
43  this.Argument = argument?.GetTypeInfo();
44  this.BaseType = baseType?.GetTypeInfo();
45  if(attribute != null)
46  {
47  this.Attribute = attribute;
48  this.NameSpace = attribute.NameSpace;
49  }
50  }
51 
54  [DataMember]
55  public TypeInfo Argument { get; set; }
56 
61  public virtual TypeInfo ArgumentStandard { get; } = typeof(object).GetTypeInfo();
62 
66  public ConverterAttribute Attribute
67  {
68  get
69  {
70  return this.attribute;
71  }
72  set
73  {
74  this.attribute = value;
75  }
76  }
77 
79  public object Base { get; set; }
80 
81 
83  [DataMember]
84  public TypeInfo BaseType { get; set; }
85 
87  public IConverterCollection Collection { get; set; }
88 
93  [DataMember]
94  public bool AllowDisambiguates { get; set; }
95 
101  [DataMember]
102  public bool DefaultValueAnyType { get; set; }
103 
105  [DataMember]
106  public TypeInfo From { get; private set; }
107 
111  [DataMember]
112  public object Function { get; set; }
113 
118  [DataMember]
119  protected MethodInfo FunctionInfo { get; set; }
120 
125  [DataMember]
126  public ConverterMethodAttribute FunctionAttribute { get; set; }
127 
134  [DataMember]
135  public object FunctionDefault { get; set; }
136 
141  [DataMember]
142  protected MethodInfo FunctionDefaultInfo { get; set; }
143 
148  [DataMember]
149  public ConverterMethodAttribute FunctionDefaultAttribute { get; set; }
150 
152  [DataMember]
153  public bool HasDefaultFunction
154  {
155  get
156  {
157  return this.FunctionDefault != null && this.FunctionDefault is Delegate;
158  }
159  }
160 
162  [DataMember]
163  public bool HasDefaultFunctionOnly
164  {
165  get
166  {
167  return this.Function == null && this.HasDefaultFunction;
168  }
169  }
170 
172  [DataMember]
173  public bool HasFunction
174  {
175  get
176  {
177  return (this.Function ?? this.FunctionDefault) != null && (this.Function ?? this.FunctionDefault) is Delegate;
178  }
179  }
180 
190  [DataMember]
191  public bool Standard
192  {
193  get
194  {
195  if(typeof(MulticastDelegate).GetTypeInfo().IsAssignableFrom(this.BaseType) == true)
196  {
197  return false;
198  }
199  return ((this.HasDefaultFunction == true) && (this.Argument == this.To))
200  || ((this.HasDefaultFunctionOnly == false) && (this.HasFunction == true) && (this.Argument == this.To || this.Argument == this.ArgumentStandard));
201  }
202  }
203 
211  [DataMember]
212  public string NameSpace { get; private set; }
213 
215  [DataMember]
216  public TypeInfo To { get; private set; }
217 
224  [DataMember]
225  public bool UseFunctionDefaultWrapper { get; set; }
226 
240  private string DebuggerDisplay
241  {
242  get
243  {
244  return this.ToString();
245  }
246  }
247 
255  public abstract object Convert(object value, object defaultValue = null);
256 
260  public override string ToString()
261  {
262  return
263  $"{this.GetType().Name}[({this.From?.Name}, {this.Argument?.Name}) => {this.To?.Name}] BaseType: {this.BaseType?.Name}, Attribute: {this.Attribute}";
264  }
265 
271  public string ToString(bool fullName)
272  {
273  if(fullName == false)
274  {
275  return this.ToString();
276  }
277 
278  return
279  $"{this.GetType().FullName}[({this.From?.FullName}, {this.Argument?.FullName}) => {this.To?.FullName}] BaseType: {this.BaseType?.FullName}, Attribute: {this.Attribute}";
280  }
281 
286  protected abstract void CheckConvertTypes(object value, object defaultValue);
287  }
288 }
string ToString(bool fullName)
Overrides object.ToString() to provide information about the conversion types
Definition: Converter.cs:271
override string ToString()
Overrides object.ToString() to provide a string representation of the underlying conversion types...
Definition: Converter.cs:260
Use ConverterAttribute to declare a class as a logical Type Converter. As such the only contingent de...
Use ConverterMethodAttribute to declare a method in an arbitrary class as a logical Converter functio...
The Base Converter interface.
Definition: IConverter.cs:31
Converter(Type from, Type to, Type argument=null, Type baseType=null, ConverterAttribute attribute=null)
Initializes a new instance of the Core.TypeCast.Base.Converter class.
Definition: Converter.cs:39
The Converter base class, providing a simple container for conversion types, ConverterAttribute and c...
Definition: Converter.cs:22