internal void ValidateInterfaceLayer(ValidationContext dslContext)
        {
            var layer = ToolkitInterfaceLayer.GetInterfaceLayer(this);

            if (layer != null)
            {
                var dataContext = new System.ComponentModel.DataAnnotations.ValidationContext(layer, this.Store, null);
                var results     = new List <System.ComponentModel.DataAnnotations.ValidationResult>();

                System.ComponentModel.DataAnnotations.Validator.TryValidateObject(layer, dataContext, results, true);

                if (results.Any())
                {
                    var properties = TypeDescriptor.GetProperties(layer);
                    var typeName   = this.Info != null ?
                                     // If we have an info, first check that it differs from the instance name
                                     // to avoid duplicating the same string
                                     this.InstanceName != this.Info.DisplayName ?
                                     this.InstanceName + @" (" + this.Info.DisplayName + @")" :
                                     // If they are equal, just use the instance name.
                                     this.InstanceName
                        : this.InstanceName;

                    foreach (var result in results)
                    {
                        // Replace member names with display names in message and member names collection.
                        var members = result.MemberNames
                                      .Select(name => new
                        {
                            Name          = name,
                            DisplayName   = properties[name].DisplayName,
                            FormattedName = typeName + @" '" + properties[name].DisplayName + @"'",
                        });

                        var message = result.ErrorMessage;

                        foreach (var member in members)
                        {
                            if (message.IndexOf(member.Name) != -1)
                            {
                                message = message.Replace(member.Name, member.FormattedName);
                            }
                            else if (message.IndexOf(member.DisplayName) != -1)
                            {
                                message = message.Replace(member.DisplayName, member.FormattedName);
                            }
                        }

                        dslContext.LogError(message,
                                            Resources.ProductElement_ValidationFailedErrorCode, this);
                    }
                }
            }
        }
        /// <summary>
        /// Attempts to convert the given object to a
        /// toolkit interface layer type or runtime type.
        /// </summary>
        public static T As <T>(this object element)
            where T : class
        {
            if (element == null)
            {
                return(default(T));
            }

            var typed = element as T;

            if (typed != null)
            {
                return(typed);
            }

            var interfaceLayer = element as IToolkitInterface;

            if (interfaceLayer != null)
            {
                // Conversion on the typed interface layer itself.
                return(interfaceLayer.As <T>());
            }

            try
            {
                var instanceBase = element as IInstanceBase;
                if (instanceBase != null)
                {
                    // Invokes the ToolkitInterfaceLayer
                    return(ToolkitInterfaceLayer.As <T>(instanceBase));
                }
            }
            catch (NotSupportedException)
            {
                var ielement = element as IElement;
                if (ielement != null)
                {
                    // Invokes the ToolkitInterfaceLayer
                    var telement = ToolkitInterfaceLayer.As <IToolkitInterface>(ielement) as T;
                    if (telement != null)
                    {
                        return(telement);
                    }
                }
            }

            return(default(T));
        }