using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.CodeDom.Compiler;

namespace Microsoft.Practices.RecipeFramework.Extensions.Converters
{
    /// <summary>
    /// A converter that validates that the input value is a valid .NET identifier (i.e. valid method,
    /// property, class name, etc.)
    /// </summary>
    /// <remarks>
    /// See <see cref="M:System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier(System.String)" />.
    /// </remarks>
    public class ValidLanguageIndependentIdentifierConverter : StringConverter
    {
        /// <summary>
        /// Returns whether the given value object is valid for this type and for the specified context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
        /// <param name="value">The <see cref="T:System.Object"></see> to test for validity.</param>
        /// <returns>
        /// true if the specified value is valid for this object; otherwise, false.
        /// </returns>
        public override bool IsValid(ITypeDescriptorContext context, object value)
        {
            if (!base.IsValid(context, value))
            {
                return false;
            }
            return CodeGenerator.IsValidLanguageIndependentIdentifier((string)value);
        }
    }
}
