//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.ComponentModel {
using System;
using System.Collections;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using Validation;
///
/// A type that generates suggested strings for Intellisense,
/// but doesn't actually convert between strings and other types.
///
public abstract class SuggestedStringsConverter : ConverterBase {
///
/// Initializes a new instance of the class.
///
protected SuggestedStringsConverter() {
}
///
/// Gets the type to reflect over for the well known values.
///
[Pure]
protected abstract Type WellKnownValuesType { get; }
///
/// Gets the values of public static fields and properties on a given type.
///
/// The type to reflect over.
/// A collection of values.
internal static ICollection GetStandardValuesForCacheShared(Type type) {
Requires.NotNull(type, "type");
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();
}
///
/// Converts a value from its string representation to its strongly-typed object.
///
/// The value.
/// The strongly-typed object.
[Pure]
protected override string ConvertFrom(string value) {
return value;
}
///
/// Creates the reflection instructions for recreating an instance later.
///
/// The value to recreate later.
///
/// The description of how to recreate an instance.
///
[Pure]
protected override InstanceDescriptor CreateFrom(string value) {
// No implementation necessary since we're only dealing with strings.
throw new NotImplementedException();
}
///
/// Converts the strongly-typed value to a string.
///
/// The value to convert.
/// The string representation of the object.
[Pure]
protected override string ConvertToString(string value) {
return value;
}
///
/// Gets the standard values to suggest with Intellisense in the designer.
///
/// A collection of the standard values.
[Pure]
protected override ICollection GetStandardValuesForCache() {
return GetStandardValuesForCacheShared(this.WellKnownValuesType);
}
}
}