コード例 #1
0
        /// <summary>
        /// Gets or sets the MetadataModel at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the MetadataModel to get or set.</param>
        /// <returns>The MetadataModel at the specified index.</returns>
        public MetadataModel this[int index]
        {
            get
            {
                if (!this.IsValueType)
                {
                    return(this.Properties[index]);
                }
                else
                {
                    throw new InvalidOperationException("This MetadataModel is a value type. Cannot access MetadataModel Properties.");
                }
            }

            set
            {
                if (!this.IsValueType)
                {
                    if (this.Properties == null)
                    {
                        this.Properties = new List <MetadataModel>();
                    }

                    int count = this.Properties.Count;

                    if (index > count - 1)
                    {
                        for (int i = 0; i <= index - count; i++)
                        {
                            this.Properties.Add(MetadataModel.GetEmptyMetadataModel());
                        }
                    }

                    this.Properties[index].CloneFrom(value, false);
                }
                else
                {
                    throw new InvalidOperationException("This MetadataModel is a value type. Cannot access MetadataModel Properties.");
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Convert object to MetadataModel instance.
        /// </summary>
        /// <param name="source">Object to convert.</param>
        /// <param name="sourceType">The type of source object.</param>
        /// <param name="name">Name for result MetadataModel.</param>
        /// <returns>Instance of MetadataModel.</returns>
        public static MetadataModel ToMetadataModel(this object source, Type sourceType, string name = null)
        {
            if (source == null)
            {
                return(MetadataModel.GetEmptyMetadataModel());
            }

            if (sourceType.Equals(typeof(MetadataModel)))
            {
                return((source as MetadataModel) ?? MetadataModel.GetEmptyMetadataModel());
            }

            MetadataModel result = new MetadataModel {
                Name = name ?? sourceType.FullName.Replace('.', '_')
            };

            if (XmlConverter.CanConvert(sourceType))
            {
                result.IsValueType = true;

                try
                {
                    result.Value = XmlConverter.ToString(source);
                }
                catch
                {
                    result.Value = null;
                }
            }
            else if (source is XmlElement)
            {
                result.IsValueType = true;

                try
                {
                    result.Value = (source as XmlElement).OuterXml;
                }
                catch
                {
                    result.Value = null;
                }
            }
            else
            {
                result.IsValueType = false;

                if (CanEnumerable(sourceType))
                {
                    if (source != null)
                    {
                        foreach (var item in source as IEnumerable)
                        {
                            result.AddProperty(item.ToMetadataModel(item.GetType()));
                        }
                    }
                    else
                    {
                        Type elementType = null;

                        if (IsDictionary(sourceType))
                        {
                            elementType = typeof(KeyValuePair <,>).MakeGenericType(sourceType.GetGenericArguments());
                        }
                        else
                        {
                            elementType = sourceType.IsArray ? sourceType.GetElementType() : sourceType.GetGenericArguments()[0];
                        }

                        result.AddProperty(((object)null).ToMetadataModel(elementType));
                    }
                }
                else
                {
                    foreach (var item in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead))
                    {
                        try
                        {
                            object itemValue = source == null ? null : item.GetValue(source, null);

                            result.AddProperty(itemValue.ToMetadataModel(item.PropertyType, item.Name));
                        }
                        catch
                        {
                        }
                    }
                }
            }

            return(result);
        }