docs
Systems
Contracts
ParamsSystem
Public API Reference

Namespace

SaffronLibrary.Prime.Systems.Param.Interfaces contains IParamKey and ParamKey<T>.

SaffronLibrary.Prime.Systems.Param contains Params.

IParamKey

The untyped contract behind every ParamKey<T>. It lets keys of different value types be stored together in one Params instance.

public interface IParamKey
{
    public string KeyName { get; }
}

Properties

KeyName

The unique string identifier for this key. Used to store or retrieve the corresponding value.

public string KeyName { get; }

ParamKey<T>

A typed key for storing and reading a value in Params. The type argument T is the type of value the key holds.

public class ParamKey<T> : IParamKey

Constructors

ParamKey(string name)

Creates a key with the given name.

public ParamKey(string name)
ParameterTypeDescription
namestringThe key's unique name.

Properties

KeyName

The key's unique name.

public string KeyName { get; }

Params

A container of typed values. Each key holds one value, and setting a key again replaces its value. The class declares no constructors, so it is created with its default constructor and starts empty.

public class Params

Methods

Has(IParamKey key)

Returns whether a value is stored under the given key.

public bool Has(IParamKey key)
ParameterTypeDescription
keyIParamKeyThe key to check.

Returns: bool. true if a value is stored under the key, otherwise false.

Set<T>(ParamKey<T> key, T objValue)

Stores a value under the given key, replacing any value already stored under it.

public void Set<T>(ParamKey<T> key, T objValue)
ParameterTypeDescription
keyParamKey<T>The key to store the value under.
objValueTThe value to store.

Get<T>(ParamKey<T> key)

Returns the value stored under the given key, as the key's type. Throws KeyNotFoundException if no value is stored under the key. Use TryGet or Has when the value may be missing.

public T Get<T>(ParamKey<T> key)
ParameterTypeDescription
keyParamKey<T>The key to read.

Returns: T. The stored value.

TryGet<T>(ParamKey<T> key, out T value)

Reads the value stored under the given key without throwing if it is missing.

public bool TryGet<T>(ParamKey<T> key, out T value)
ParameterTypeDescription
keyParamKey<T>The key to read.
valueout TThe stored value, or the type's default if the key has no value.

Returns: bool. true if a value was stored under the key, otherwise false.

ToDictionary()

Returns a new dictionary containing the stored values, keyed by each key's name.

public Dictionary<string, object> ToDictionary()

Returns: Dictionary<string, object>. A copy of the stored values keyed by name.

Require<TValue>(Params p, ParamKey<TValue> key)

Returns the value stored under the given key. Throws InvalidOperationException if the key has no value or the value has the wrong type.

public static TValue Require<TValue>(Params p, ParamKey<TValue> key)
ParameterTypeDescription
pParamsThe params to read.
keyParamKey<TValue>The key to read.

Returns: TValue. The stored value.

RequireObject<TValue>(Params p, ParamKey<TValue> key)

Returns the reference-type value stored under the given key. Throws InvalidOperationException if the key has no value or the value has the wrong type.

public static TValue RequireObject<TValue>(Params p, ParamKey<TValue> key) where TValue : class
ParameterTypeDescription
pParamsThe params to read.
keyParamKey<TValue>The key to read.

Returns: TValue. The stored value.

RequireObject<T>(Params p, ParamKey<object> key)

Returns the value stored under an object-typed key, cast to T. Throws ArgumentNullException if p or key is null. Throws InvalidOperationException if the key has no value or the value is not a T.

public static T RequireObject<T>(Params p, ParamKey<object> key) where T : class
ParameterTypeDescription
pParamsThe params to read.
keyParamKey<object>The object-typed key to read.

Returns: T. The stored value, cast to T.