Search Results for

    Show / Hide Table of Contents

    Interface IPropertyBag

    A PropertyBag represents a collection of name/value pairs that allows duplicate entries with the same key. Methods are provided for adding a new pair as well as for setting a key to a single value. All keys are strings but values may be of any type. Null values are not permitted, since a null entry represents the absence of the key.

    The entries in a PropertyBag are of two kinds: those that take a single value and those that take multiple values. However, the PropertyBag has no knowledge of which entries fall into each category and the distinction is entirely up to the code using the PropertyBag.

    When working with multi-valued properties, client code should use the Add method to add name/value pairs and indexing to retrieve a list of all values for a given key. For example:

    bag.Add("Tag", "one");
    bag.Add("Tag", "two");
    Assert.That(bag["Tag"],
      Is.EqualTo(new string[] { "one", "two" }));
    

    When working with single-valued properties, client code should use the Set method to set the value and Get to retrieve the value. The GetSetting methods may also be used to retrieve the value in a type-safe manner while also providing default. For example:

    bag.Set("Priority", "low");
    bag.Set("Priority", "high"); // replaces value
    Assert.That(bag.Get("Priority"),
      Is.EqualTo("high"));
    Assert.That(bag.GetSetting("Priority", "low"),
      Is.EqualTo("high"));
    
    Inherited Members
    IXmlNodeBuilder.ToXml(bool)
    IXmlNodeBuilder.AddToXml(TNode, bool)
    Namespace: NUnit.Framework.Interfaces
    Assembly: nunit.framework.dll
    Syntax
    public interface IPropertyBag : IXmlNodeBuilder

    Properties

    View Source

    this[string]

    Gets or sets the list of values for a particular key, initializes new list behind the key if not found.

    Declaration
    IList this[string key] { get; set; }
    Parameters
    Type Name Description
    string key

    The key for which the values are to be retrieved or set

    Property Value
    Type Description
    IList
    View Source

    Keys

    Gets a collection containing all the keys in the property set

    Declaration
    ICollection<string> Keys { get; }
    Property Value
    Type Description
    ICollection<string>

    Methods

    View Source

    Add(string, object)

    Adds a key/value pair to the property bag

    Declaration
    void Add(string key, object value)
    Parameters
    Type Name Description
    string key

    The key

    object value

    The value

    View Source

    ContainsKey(string)

    Gets a flag indicating whether the specified key has any entries in the property set.

    Declaration
    bool ContainsKey(string key)
    Parameters
    Type Name Description
    string key

    The key to be checked

    Returns
    Type Description
    bool

    True if their are values present, otherwise false

    View Source

    Get(string)

    Gets a single value for a key, using the first one if multiple values are present and returning null if the value is not found.

    Declaration
    object? Get(string key)
    Parameters
    Type Name Description
    string key
    Returns
    Type Description
    object
    View Source

    Set(string, object)

    Sets the value for a key, removing any other values that are already in the property set.

    Declaration
    void Set(string key, object value)
    Parameters
    Type Name Description
    string key
    object value
    View Source

    TryGet(string, out IList?)

    Tries to retrieve list of values.

    Declaration
    bool TryGet(string key, out IList? values)
    Parameters
    Type Name Description
    string key

    The key for which the values are to be retrieved

    IList values

    Values, if found

    Returns
    Type Description
    bool

    true if found

    • View Source
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0