Click or drag to resize

AbstractCollectionTypeProto Class

Base class for a type proto handler that can deal with collections.
Inheritance Hierarchy

Namespace:  KSPDev.ConfigUtils
Assembly:  KSPDev_Utils.1.2 (in KSPDev_Utils.1.2.dll) Version: 1.2 for KSP v1.6+
Syntax
C#
public abstract class AbstractCollectionTypeProto
Request Example View Source

The AbstractCollectionTypeProto type exposes the following members.

Constructors
  NameDescription
Protected methodAbstractCollectionTypeProto
Initializes a new instance of the AbstractCollectionTypeProto class
Top
Methods
  NameDescription
Public methodAddItem
Adds an item into the collection.
Public methodClearItems
Removes all items from the collection.
Public methodGetEnumerator
Returns enumerable object for the collection.
Public methodGetItemType
Returns type of items in the collection.
Top
Remarks
Collection of collections is not supported. However, descendands may use own (de)serialization approach to handle nested collections.

All descendants of this class must implement a constructor which accepts a single argument: the type of the collection. Constructor can throw ArgumentException if passed type is unacceptable.

Examples
As a good example of overriding of this class see GenericCollectionTypeProto. Though, it tries to be universal and, hence, works via reflection. You don't need to deal with reflections as long as your custom proto used for the fields of known types only.
class MyBooleanCollection {
  public void AddItem(bool itemValue) {
    // ...some custom code...
  }
  public IEnumerable GetMyVeryCustomIterator() {
    // ...some custom code...
    return res;
  }
}

class MyBooleanCollectionProto : AbstractCollectionTypeProto {
  public MyBooleanCollectionProto() : base(typeof(bool)) {}

  public override Type GetItemType() {
    return typeof(bool);
  }
  public override IEnumerable GetEnumerator(object instance) {
    return (instance as MyBooleanCollection).GetMyVeryCustomIterator(); 
  }
  public override void AddItem(object instance, object item) {
    (instance as MyBooleanCollection).AddItem((bool) item);
  }
}
See Also