summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core.UI/ComponentModel/SuggestedStringsConverter.cs
blob: 62a4b137adc32adef9258621f1ed533ee7da60da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//-----------------------------------------------------------------------
// <copyright file="SuggestedStringsConverter.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.ComponentModel {
	using System;
	using System.Collections;
	using System.ComponentModel.Design.Serialization;
	using System.Diagnostics.Contracts;
	using System.Linq;
	using System.Reflection;

	/// <summary>
	/// A type that generates suggested strings for Intellisense,
	/// but doesn't actually convert between strings and other types.
	/// </summary>
	[ContractClass(typeof(SuggestedStringsConverterContract))]
	public abstract class SuggestedStringsConverter : ConverterBase<string> {
		/// <summary>
		/// Initializes a new instance of the <see cref="SuggestedStringsConverter"/> class.
		/// </summary>
		protected SuggestedStringsConverter() {
		}

		/// <summary>
		/// Gets the type to reflect over for the well known values.
		/// </summary>
		[Pure]
		protected abstract Type WellKnownValuesType { get; }

		/// <summary>
		/// Gets the values of public static fields and properties on a given type.
		/// </summary>
		/// <param name="type">The type to reflect over.</param>
		/// <returns>A collection of values.</returns>
		internal static ICollection GetStandardValuesForCacheShared(Type type) {
			Requires.NotNull(type, "type");
			Contract.Ensures(Contract.Result<ICollection>() != null);

			var fields = from field in type.GetFields(BindingFlags.Static | BindingFlags.Public)
						 select field.GetValue(null);
			var properties = from prop in type.GetProperties(BindingFlags.Static | BindingFlags.Public)
							 select prop.GetValue(null, null);
			return fields.Concat(properties).ToArray();
		}

		/// <summary>
		/// Converts a value from its string representation to its strongly-typed object.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns>The strongly-typed object.</returns>
		[Pure]
		protected override string ConvertFrom(string value) {
			return value;
		}

		/// <summary>
		/// Creates the reflection instructions for recreating an instance later.
		/// </summary>
		/// <param name="value">The value to recreate later.</param>
		/// <returns>
		/// The description of how to recreate an instance.
		/// </returns>
		[Pure]
		protected override InstanceDescriptor CreateFrom(string value) {
			// No implementation necessary since we're only dealing with strings.
			throw new NotImplementedException();
		}

		/// <summary>
		/// Converts the strongly-typed value to a string.
		/// </summary>
		/// <param name="value">The value to convert.</param>
		/// <returns>The string representation of the object.</returns>
		[Pure]
		protected override string ConvertToString(string value) {
			return value;
		}

		/// <summary>
		/// Gets the standard values to suggest with Intellisense in the designer.
		/// </summary>
		/// <returns>A collection of the standard values.</returns>
		[Pure]
		protected override ICollection GetStandardValuesForCache() {
			return GetStandardValuesForCacheShared(this.WellKnownValuesType);
		}
	}
}