Class JsonRegistry<TKey, TValue>
Represents a collection of values with unique IDs that can be loaded from JSON.
Inheritance
Inherited Members
Namespace: SlugBase
Assembly: SlugBase.dll
Syntax
public class JsonRegistry<TKey, TValue>
where TKey : ExtEnum<TKey>
Type Parameters
Name | Description |
---|---|
TKey | The type of the ExtEnum`1 keys. |
TValue | The type of the values. |
Constructors
JsonRegistry(Func<TKey, JsonObject, TValue>)
Create a new registry.
Declaration
public JsonRegistry(Func<TKey, JsonObject, TValue> fromJson)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TKey, JsonObject, TValue> | fromJson | The factory that creates values from JSON. |
Properties
Keys
A collection of all keys used by this registry.
Declaration
public IEnumerable<TKey> Keys { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TKey> |
Values
A collection of all values registered.
Declaration
public IEnumerable<TValue> Values { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TValue> |
WatchForChanges
Whether this registry should track files to reload. Call ReloadChangedFiles() to apply changes.
Declaration
public bool WatchForChanges { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
Methods
Add(JsonObject)
Load a TValue
from JSON and link it to a new ExtEnum`1 ID.
Declaration
public KeyValuePair<TKey, TValue> Add(JsonObject json)
Parameters
Type | Name | Description |
---|---|---|
JsonObject | json | The json data for the new object. |
Returns
Type | Description |
---|---|
System.Collections.Generic.KeyValuePair<TKey, TValue> | The registered key and value. |
AddFromFile(String)
Parse a file as JSON and link it to a new ExtEnum`1 ID.
Declaration
public KeyValuePair<TKey, TValue> AddFromFile(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The file path to the json. |
Returns
Type | Description |
---|---|
System.Collections.Generic.KeyValuePair<TKey, TValue> | The registered key and value. |
GetOrDefault(TKey)
Get a registered value by key.
Declaration
public TValue GetOrDefault(TKey id)
Parameters
Type | Name | Description |
---|---|---|
TKey | id | The unique ID of the value. |
Returns
Type | Description |
---|---|
TValue | The registered value with the unique ID. |
ReloadChangedFiles()
Reload all JSON files that have been modified.
Declaration
public void ReloadChangedFiles()
Remove(TKey)
Unregister a value.
Declaration
public void Remove(TKey id)
Parameters
Type | Name | Description |
---|---|---|
TKey | id |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | |
System.ArgumentException |
ScanDirectory(String)
Register all JSON files in a directory using AddFromFile(String).
Declaration
public void ScanDirectory(string directory)
Parameters
Type | Name | Description |
---|---|---|
System.String | directory | The directory to search. |
TryAddFromFile(String)
Parse a file as JSON and link it to a new ExtEnum`1 ID. Load errors can be monitored with LoadFailed.
Declaration
public KeyValuePair<TKey, TValue>? TryAddFromFile(string path)
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The file path to the json. |
Returns
Type | Description |
---|---|
System.Nullable<System.Collections.Generic.KeyValuePair<TKey, TValue>> | The registered key and value, or |
TryGet(TKey, out TValue)
Get a registered value by key.
Declaration
public bool TryGet(TKey id, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | id | The unique ID of the value. |
TValue | value | The registered value with the unique ID. |
Returns
Type | Description |
---|---|
System.Boolean |
|
TryGetPath(TKey, out String)
Gets the path to a registered value by key.
Declaration
public bool TryGetPath(TKey id, out string path)
Parameters
Type | Name | Description |
---|---|---|
TKey | id | The unique ID of the value. |
System.String | path | The path to the value's JSON file. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Events
EntryReloaded
Occurs when a loaded JSON file is modified while WatchForChanges is set.
Declaration
public event EventHandler<JsonRegistry<TKey, TValue>.ReloadedEventArgs> EntryReloaded
Event Type
Type | Description |
---|---|
System.EventHandler<JsonRegistry.ReloadedEventArgs<>> |
LoadFailed
Occurs when a JSON file fails to load or reload.
Declaration
public event EventHandler<JsonRegistry<TKey, TValue>.LoadErrorEventArgs> LoadFailed
Event Type
Type | Description |
---|---|
System.EventHandler<JsonRegistry.LoadErrorEventArgs<>> |