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> : IParamKeyConstructors
ParamKey(string name)
Creates a key with the given name.
public ParamKey(string name)| Parameter | Type | Description |
|---|---|---|
name | string | The 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 ParamsMethods
Has(IParamKey key)
Returns whether a value is stored under the given key.
public bool Has(IParamKey key)| Parameter | Type | Description |
|---|---|---|
key | IParamKey | The 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)| Parameter | Type | Description |
|---|---|---|
key | ParamKey<T> | The key to store the value under. |
objValue | T | The 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)| Parameter | Type | Description |
|---|---|---|
key | ParamKey<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)| Parameter | Type | Description |
|---|---|---|
key | ParamKey<T> | The key to read. |
value | out T | The 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)| Parameter | Type | Description |
|---|---|---|
p | Params | The params to read. |
key | ParamKey<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| Parameter | Type | Description |
|---|---|---|
p | Params | The params to read. |
key | ParamKey<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| Parameter | Type | Description |
|---|---|---|
p | Params | The params to read. |
key | ParamKey<object> | The object-typed key to read. |
Returns: T. The stored value, cast to T.