diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2011-07-01 16:49:44 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2011-07-01 16:49:44 -0700 |
commit | b6f7a18b949acb4346754ae47fb07424076a3cd0 (patch) | |
tree | 4c23cb2b8174f3288cb0b787cff4c6ac432c6bef /src/DotNetOpenAuth.Messaging/ComponentModel/SuggestedStringsConverter.cs | |
parent | f16525005555b86151b7a1c741aa29550635108a (diff) | |
download | DotNetOpenAuth-b6f7a18b949acb4346754ae47fb07424076a3cd0.zip DotNetOpenAuth-b6f7a18b949acb4346754ae47fb07424076a3cd0.tar.gz DotNetOpenAuth-b6f7a18b949acb4346754ae47fb07424076a3cd0.tar.bz2 |
First pass at dividing DotNetOpenAuth features into separate assemblies.
Nothing compiles at this point.
Diffstat (limited to 'src/DotNetOpenAuth.Messaging/ComponentModel/SuggestedStringsConverter.cs')
-rw-r--r-- | src/DotNetOpenAuth.Messaging/ComponentModel/SuggestedStringsConverter.cs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Messaging/ComponentModel/SuggestedStringsConverter.cs b/src/DotNetOpenAuth.Messaging/ComponentModel/SuggestedStringsConverter.cs new file mode 100644 index 0000000..864d001 --- /dev/null +++ b/src/DotNetOpenAuth.Messaging/ComponentModel/SuggestedStringsConverter.cs @@ -0,0 +1,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) { + Contract.Requires<ArgumentNullException>(type != null); + 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); + } + } +} |