public class XmlElementAttributes : CollectionBase
|
Transportation
class, which contains a single field named
Vehicles
that returns an ArrayList. The example first applies two instances of the XmlElementAttribute class to the
Vehicles
field that specify the types of objects the XmlSerializer can insert into the array. The example then creates two XmlElementAttribute objects to override the behavior of the attributes applied to the Vehicles property. The two overriding objects are added to the XmlElementAttributes collection of an XmlAttributes. Lastly, the example adds the XmlAttributes to an XmlAttributeOverrides, allowing the XmlSerializer to insert the new object types into the ArrayList returned by the
Vehicles
field.using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; public class Transportation { // Override these two XmlElementAttributes. [XmlElement(typeof(Car)), XmlElement(typeof(Plane))] public ArrayList Vehicles; } public class Car { public string Name; } public class Plane { public string Name; } public class Truck { public string Name; } public class Train { public string Name; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("OverrideElement.xml"); } public XmlSerializer CreateOverrider() { // Create XmlAtrributes and XmlAttributeOverrides instances. XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Create an XmlElementAttributes object to override one of the attributes applied to the Vehicles property. */ XmlElementAttribute xElement1 = new XmlElementAttribute(typeof(Truck)); // Add the XmlElementAttribute to the collection. attrs.XmlElements.Add(xElement1); /* Create a second XmlElementAttribute and add it to the collection. */ XmlElementAttribute xElement2 = new XmlElementAttribute(typeof(Train)); attrs.XmlElements.Add(xElement2); /* Add the XmlAttributes to the XmlAttributeOverrides, specifying the member to override. */ xOver.Add(typeof(Transportation), "Vehicles", attrs); // Create the XmlSerializer, and return it. XmlSerializer xSer = new XmlSerializer (typeof(Transportation), xOver); return xSer; } public void SerializeObject(string filename) { // Create an XmlSerializer instance. XmlSerializer xSer = CreateOverrider(); // Create the object. Transportation myTransportation = new Transportation(); /* Create two new, overriding objects that can be inserted into the Vehicles array. */ myTransportation.Vehicles = new ArrayList(); Truck myTruck = new Truck(); myTruck.Name = "MyTruck"; Train myTrain = new Train(); myTrain.Name = "MyTrain"; myTransportation.Vehicles.Add(myTruck); myTransportation.Vehicles.Add(myTrain); TextWriter writer = new StreamWriter(filename); xSer.Serialize(writer, myTransportation); } }
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. |
Count (inherited from System.Collections.CollectionBase) |
Read-only See base class member description: System.Collections.CollectionBase.Count Gets the number of elements contained in the CollectionBase instance. |
Item | Read-write Gets or sets an XmlElementAttribute from the collection. |
Add | Adds an XmlElementAttribute to the collection. |
Clear (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.Clear Removes all objects from the CollectionBase instance. |
Contains | Gets a value that specifies whether the collections contains the specified object. |
CopyTo | Copies the XmlElementAttributes, or a portion of it to a one-dimensional array. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetEnumerator (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.GetEnumerator Returns an enumerator that can iterate through the CollectionBase instance. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IndexOf | Gets the index of the specified XmlElementAttribute. |
Insert | Inserts an XmlElementAttribute into the collection. |
Remove | Removes the specified object from the collection. |
RemoveAt (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.RemoveAt Removes the element at the specified index of the CollectionBase instance. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
InnerList (inherited from System.Collections.CollectionBase) |
Read-only See base class member description: System.Collections.CollectionBase.InnerList Gets an ArrayList containing the list of elements in the CollectionBase instance. |
List (inherited from System.Collections.CollectionBase) |
Read-only See base class member description: System.Collections.CollectionBase.List Gets an IList containing the list of elements in the CollectionBase instance. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
OnClear (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnClear Performs additional custom processes when clearing the contents of the CollectionBase instance. |
OnClearComplete (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnClearComplete Performs additional custom processes after clearing the contents of the CollectionBase instance. |
OnInsert (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnInsert Performs additional custom processes before inserting a new element into the CollectionBase instance. |
OnInsertComplete (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnInsertComplete Performs additional custom processes after inserting a new element into the CollectionBase instance. |
OnRemove (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnRemove Performs additional custom processes when removing an element from the CollectionBase instance. |
OnRemoveComplete (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnRemoveComplete Performs additional custom processes after removing an element from the CollectionBase instance. |
OnSet (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnSet Performs additional custom processes before setting a value in the CollectionBase instance. |
OnSetComplete (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnSetComplete Performs additional custom processes after setting a value in the CollectionBase instance. |
OnValidate (inherited from System.Collections.CollectionBase) |
See base class member description: System.Collections.CollectionBase.OnValidate Performs additional custom processes when validating a value. |
Hierarchy:
public XmlElementAttributes(); |
public int Count {get;}
|
protected ArrayList InnerList {get;}
|
public XmlElementAttribute this[int index] {get; set;}
|
index
private void GetXmlElement(XmlAttributes attrs) { // Get the first XmlElementAttribute in the collection. XmlElementAttribute att = attrs.XmlElements[0]; // Get the Type of the XmlElementAttribute and print it. Type t = att.Type; Console.WriteLine(t.ToString()); }
protected IList List {get;}
|
public int Add( |
attribute
Transportation
class.public XmlSerializer CreateOverrider() { // Create XmlAttributes and XmlAttributeOverrides instances. XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Create an XmlElementAttributes to override the Vehicles property. */ XmlElementAttribute xElement1 = new XmlElementAttribute(typeof(Truck)); // Add the XmlElementAttribute to the collection. attrs.XmlElements.Add(xElement1); /* Create a second XmlElementAttribute, and add to the collection. */ XmlElementAttribute xElement2 = new XmlElementAttribute(typeof(Train)); attrs.XmlElements.Add(xElement2); /* Add the XmlAttributes to the XmlAttributeOverrides, specifying the member to override. */ xOver.Add(typeof(Transportation), "Vehicles", attrs); // Create the XmlSerializer, and return it. XmlSerializer xSer = new XmlSerializer (typeof(Transportation), xOver); return xSer; }
public void Clear(); |
public bool Contains( |
attribute
public void CopyTo( |
array
index
~XmlElementAttributes(); |
public IEnumerator GetEnumerator(); |
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
public virtual int GetHashCode(); |
public Type GetType(); |
public int IndexOf( |
attribute
public void Insert( |
index
attribute
protected object MemberwiseClone(); |
protected virtual void OnClear(); |
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed before deleting all the elements from the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnClear is invoked before the standard Clear behavior, whereas CollectionBase.OnClearComplete is invoked after the standard Clear behavior.
For example, implementers can exempt certain elements from deletion by a global Clear.
protected virtual void OnClearComplete(); |
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed after deleting all the elements from the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnClear is invoked before the standard Clear behavior, whereas CollectionBase.OnClearComplete is invoked after the standard Clear behavior.
index
value
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed before inserting the element into the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnInsert is invoked before the standard Insert behavior, whereas CollectionBase.OnInsertComplete is invoked after the standard Insert behavior.
For example, implementers can restrict which types of objects can be inserted into the ArrayList.
index
value
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed after inserting the element into the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnInsert is invoked before the standard Insert behavior, whereas CollectionBase.OnInsertComplete is invoked after the standard Insert behavior.
index
value
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed before removing the element from the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnRemove is invoked before the standard Remove behavior, whereas CollectionBase.OnRemoveComplete is invoked after the standard Remove behavior.
For example, implementers can prevent removal of elements by always throwing an exception in CollectionBase.OnRemove.
index
value
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed after removing the element from the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnRemove is invoked before the standard Remove behavior, whereas CollectionBase.OnRemoveComplete is invoked after the standard Remove behavior.
index
oldValue
newValue
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed before setting the specified element in the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnSet is invoked before the standard Set behavior, whereas CollectionBase.OnSetComplete is invoked after the standard Set behavior.
For example, implementers can restrict which values can be overwritten by performing a check inside CollectionBase.OnSet.
index
oldValue
newValue
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed after setting the specified element in the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnSet is invoked before the standard Set behavior, whereas CollectionBase.OnSetComplete is invoked after the standard Set behavior.
protected virtual void OnValidate( |
value
The On* methods are invoked only on the instance returned by the CollectionBase.List property, but not on the instance returned by the CollectionBase.InnerList property.
This method allows implementers to define processes that must be performed when executing the standard behavior of the underlying ArrayList. By defining this method, implementers can add functionality to inherited methods without having to override all other methods.
CollectionBase.OnValidate can be used to impose restrictions on the type of objects that are accepted into the collection. The default implementation prevents null from being added to or removed from the underlying ArrayList.
public void Remove( |
attribute
public void RemoveAt( |
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index is equal to or greater than CollectionBase.Count. |
public virtual string ToString(); |