IsCollection() публичный статический Метод

Determines whether a Type is a Collection type.
public static IsCollection ( Type type ) : bool
type System.Type
Результат bool
Пример #1
0
        /// <summary>
        /// Serializes the properties an Object and appends them to the specified XmlNode.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="parent"></param>
        protected void SerializeProperties(object obj, XmlNode parent)
        {
            if (TypeInfo.IsCollection(obj.GetType()))
            {
                SetCollectionItems(obj, (ICollection)obj, parent);
            }
            else
            {
                XmlElement node = parent.OwnerDocument.CreateElement(taglib.PROPERTIES_TAG);

                SetProperties(obj, node);

                parent.AppendChild(node);
            }
            objlist.Remove(obj);
        }
Пример #2
0
        /// <summary>
        /// Reads the properties of the specified node and sets them an the parent object.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="node"></param>
        /// <remarks>
        /// This is the central method which is called recursivly!
        /// </remarks>
        protected void GetProperties(object parent, XmlNode node)
        {
            if (parent == null)
            {
                return;
            }

            // Get the properties
            XmlNodeList nl = node.SelectNodes(taglib.PROPERTIES_TAG + "/" + taglib.PROPERTY_TAG);

            // Properties found?
            if (nl == null || nl.Count == 0)
            {
                // No properties found... perhaps a collection?
                if (TypeInfo.IsCollection(parent.GetType()))
                {
                    SetCollectionValues((ICollection)parent, node);
                }
                else
                {
                    // Nothing to do here
                    return;
                }
            }

            // Loop the properties found
            foreach (XmlNode prop in nl)
            {
                // Collect the nodes type information about the property to deserialize
                ObjectInfo oi = GetObjectInfo(prop);

                // Enough info?
                if (oi.IsSufficient && !String.IsNullOrEmpty(oi.Name))
                {
                    object obj;

                    // Create an instance, but note: arrays always need the size for instantiation
                    if (TypeInfo.IsArray(oi.Type))
                    {
                        int c = GetArrayLength(prop);
                        obj = CreateArrayInstance(oi, c);
                    }
                    else
                    {
                        obj = CreateInstance(oi);
                    }

                    // Process the property's properties (recursive call of this method)
                    if (obj != null)
                    {
                        GetProperties(obj, prop);
                    }

                    // Setting the instance (or null) as the property's value
                    PropertyInfo pi = parent.GetType().GetProperty(oi.Name);
                    if (obj != null && pi != null)
                    {
                        pi.SetValue(parent, obj, null);
                    }
                }
            }

            return;
        }
Пример #3
0
        /// <summary>
        /// Sets the items on a collection.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="value"></param>
        /// <param name="parent"></param>
        /// <remarks>
        /// This method could be simplified since it's mainly the same code you can find in SetProperty()
        /// </remarks>
        protected void SetCollectionItems(object obj, ICollection value, XmlNode parent)
        {
            // Validating the parameters
            if (obj == null || value == null || parent == null)
            {
                return;
            }

            try
            {
                ICollection coll = value;

                XmlElement collnode = parent.OwnerDocument.CreateElement(taglib.ITEMS_TAG);
                parent.AppendChild(collnode);

                int cnt = 0;

                // What kind of Collection?
                if (TypeInfo.IsDictionary(coll.GetType()))
                {
                    // IDictionary
                    var dict = (IDictionary)coll;
                    IDictionaryEnumerator de = dict.GetEnumerator();
                    while (de.MoveNext())
                    {
                        object key = de.Key;
                        object val = de.Value;

                        XmlElement itemnode = parent.OwnerDocument.CreateElement(taglib.ITEM_TAG);
                        collnode.AppendChild(itemnode);

                        object curr = de.Current;

                        XmlElement propsnode = parent.OwnerDocument.CreateElement(taglib.PROPERTIES_TAG);
                        itemnode.AppendChild(propsnode);

                        SetProperties(curr, propsnode);
                    }
                }
                else
                {
                    // Everything else
                    IEnumerator ie = coll.GetEnumerator();
                    while (ie.MoveNext())
                    {
                        object obj2 = ie.Current;

                        XmlElement itemnode = parent.OwnerDocument.CreateElement(taglib.ITEM_TAG);

                        if (obj2 != null)
                        {
                            SetObjectInfoAttributes(null, obj2.GetType(), itemnode);
                        }
                        else
                        {
                            SetObjectInfoAttributes(null, null, itemnode);
                        }

                        itemnode.Attributes[taglib.NAME_TAG].Value = "" + cnt;

                        cnt++;

                        collnode.AppendChild(itemnode);

                        if (obj2 == null)
                        {
                            continue;
                        }

                        Type pt = obj2.GetType();

                        if (TypeInfo.IsCollection(pt))
                        {
                            SetCollectionItems(obj, (ICollection)obj2, itemnode);
                        }
                        else
                        {
                            SetXmlElementFromBasicPropertyValue(itemnode, pt, obj2, parent);
                        }         // IsCollection?
                    }             // Loop collection
                }                 // IsDictionary?
            }
            catch (Exception exc)
            {
                if (!IgnoreSerialisationErrors)
                {
                    throw exc;
                }
                else
                {
                    // perhaps logging
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Sets a property.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="value"></param>
        /// <param name="pi"></param>
        /// <param name="parent"></param>
        /// <remarks>
        /// This is the central method which is called recursivly!
        /// </remarks>
        protected void SetProperty(object obj, object value, PropertyInfo pi, XmlNode parent)
        {
            //      object val = value; ??

            try
            {
                // Empty values are ignored (no need to restore null references or empty Strings)
                if (value == null || value.Equals(string.Empty))
                {
                    return;
                }

                // Get the Type
                //Type pt = pi.PropertyType;
                Type pt = value.GetType();

                // Check whether this property can be serialized and deserialized
                if (CheckPropertyHasToBeSerialized(pi) && (pt.IsSerializable || IgnoreSerializableAttribute) && (pi.CanWrite) &&
                    ((pt.IsPublic) || (pt.IsEnum)))
                {
                    XmlElement prop = parent.OwnerDocument.CreateElement(taglib.PROPERTY_TAG);

                    SetObjectInfoAttributes(pi.Name, pt, prop);

                    // Try to find a constructor for binary data.
                    // If found remember the parameter's Type.
                    Type binctortype = TypeInfo.GetBinaryConstructorType(pt);

                    if (binctortype != null)                     // a binary contructor was found
                    {
                        /*
                         * b. Trying to handle binary data
                         */

                        SerializeBinaryObject(pi.GetValue(obj, null), binctortype, prop);
                    }
                    else if (TypeInfo.IsCollection(pt))
                    {
                        /*
                         * a. Collections ask for a specific handling
                         */
                        SetCollectionItems(obj, (ICollection)value, prop);
                    }
                    else
                    {
                        /*
                         * c. "normal" classes
                         */

                        SetXmlElementFromBasicPropertyValue(prop, pt, value, parent);
                    }
                    // Append the property node to the paren XmlNode
                    parent.AppendChild(prop);
                }
            }
            catch (Exception)
            {
                if (!IgnoreSerialisationErrors)
                {
                    throw;
                }
            }
        }