System.Xml.Serialization.TypeData.GetIndexerProperty C# (CSharp) Method

GetIndexerProperty() public static method

public static GetIndexerProperty ( Type collectionType ) : PropertyInfo
collectionType System.Type
return System.Reflection.PropertyInfo
		public static PropertyInfo GetIndexerProperty (Type collectionType)
		{
			PropertyInfo[] props = collectionType.GetProperties (BindingFlags.Instance | BindingFlags.Public);
			foreach (PropertyInfo prop in props)
			{
				ParameterInfo[] pi = prop.GetIndexParameters ();
				if (pi != null && pi.Length == 1 && pi[0].ParameterType == typeof(int))
					return prop;
			}
			return null;
		}

Usage Example

 void WriteListContent(object container, TypeData listType, ListMap map, object ob, StringBuilder targetString)
 {
     if (listType.Type.IsArray)
     {
         Array array = (Array)ob;
         for (int n = 0; n < array.Length; n++)
         {
             object item = array.GetValue(n);
             XmlTypeMapElementInfo info = map.FindElement(container, n, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else if (ob is ICollection)
     {
         int          count    = (int)ob.GetType().GetProperty("Count").GetValue(ob, null);
         PropertyInfo itemProp = TypeData.GetIndexerProperty(listType.Type);
         object[]     index    = new object[1];
         for (int n = 0; n < count; n++)
         {
             index[0] = n;
             object item = itemProp.GetValue(ob, index);
             XmlTypeMapElementInfo info = map.FindElement(container, n, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else if (ob is IEnumerable)
     {
         IEnumerable e = (IEnumerable)ob;
         foreach (object item in e)
         {
             XmlTypeMapElementInfo info = map.FindElement(container, -1, item);
             if (info != null && targetString == null)
             {
                 WriteMemberElement(info, item);
             }
             else if (info != null && targetString != null)
             {
                 targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" ");
             }
             else if (item != null)
             {
                 throw CreateUnknownTypeException(item);
             }
         }
     }
     else
     {
         throw new Exception("Unsupported collection type");
     }
 }
All Usage Examples Of System.Xml.Serialization.TypeData::GetIndexerProperty