示例#1
0
        /// <summary>
        /// Convert a NameValueCollection-Like List to a Dictionary of IAttributes
        /// </summary>
        public static Dictionary <string, IAttribute> ConvertToAttributes(this IDictionary <string, object> objAttributes)
        {
            var result = new Dictionary <string, IAttribute>(StringComparer.OrdinalIgnoreCase);

            foreach (var oAttrib in objAttributes)
            {
                // in case the object is already an IAttribute, use that, don't rebuild it
                if (oAttrib.Value is IAttribute)
                {
                    result[oAttrib.Key] = (IAttribute)oAttrib.Value;
                }
                else
                {
                    var attributeType = GetAttributeTypeName(oAttrib.Value);
                    //var baseModel = new AttributeDefinition(attribute.Key, attributeType, attribute.Key == titleAttributeName, 0, 0);
                    var attributeModel  = AttributeBase.CreateTypedAttribute(oAttrib.Key, attributeType);//  baseModel.CreateAttribute();
                    var valuesModelList = new List <IValue>();
                    if (oAttrib.Value != null)
                    {
                        var valueModel = Value.Build(attributeType, oAttrib.Value, null);
                        valuesModelList.Add(valueModel);
                    }

                    attributeModel.Values = valuesModelList;

                    result[oAttrib.Key] = attributeModel;
                }
            }

            return(result);

            // helper to get text-name of the type
            string GetAttributeTypeName(object value)
            {
                if (value is DateTime)
                {
                    return("DateTime");
                }
                if (value is decimal || value is int || value is double)
                {
                    return("Number");
                }
                if (value is bool)
                {
                    return("Boolean");
                }
                if (value is Guid || value is List <Guid> || value is List <Guid?> || value is List <int> || value is List <int?> )
                {
                    return("Entity");
                }
                if (value is int[] || value is int?[])