CapyKit/Docs/api/CapyKit.Helpers.SettingsHelper.md

137 lines
5 KiB
Markdown

# <a id="CapyKit_Helpers_SettingsHelper"></a> Class SettingsHelper
Namespace: [CapyKit.Helpers](CapyKit.Helpers.md)
Assembly: CapyKit.dll
Static class containing helper methods for retrieving and setting application settings.
```csharp
public static class SettingsHelper
```
#### Inheritance
[object](https://learn.microsoft.com/dotnet/api/system.object) ←
[SettingsHelper](CapyKit.Helpers.SettingsHelper.md)
#### Inherited Members
[object.Equals\(object?\)](https://learn.microsoft.com/dotnet/api/system.object.equals\#system\-object\-equals\(system\-object\)),
[object.Equals\(object?, object?\)](https://learn.microsoft.com/dotnet/api/system.object.equals\#system\-object\-equals\(system\-object\-system\-object\)),
[object.GetHashCode\(\)](https://learn.microsoft.com/dotnet/api/system.object.gethashcode),
[object.GetType\(\)](https://learn.microsoft.com/dotnet/api/system.object.gettype),
[object.MemberwiseClone\(\)](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone),
[object.ReferenceEquals\(object?, object?\)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals),
[object.ToString\(\)](https://learn.microsoft.com/dotnet/api/system.object.tostring)
#### Extension Methods
[ObjectExtensions.UpdateProperties\(object, object\)](CapyKit.Extensions.ObjectExtensions.md\#CapyKit\_Extensions\_ObjectExtensions\_UpdateProperties\_System\_Object\_System\_Object\_)
## Examples
This example demonstrates how to set up the SettingsHelper class with custom accessor and
detector methods that read from an App.config file. The setup is done at the beginning of the
application execution, before any other usage of the helper methods.
<pre><code class="lang-csharp">public int main(string[] args)
{
// Set up SettingsHelper with custom accessor and detector methods
Func&lt;string, object&gt; accessor = (key) =&gt;
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
return config.AppSettings.Settings[key].Value;
};
Func&lt;string, bool&gt; detector = (key) =&gt;
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
return config.AppSettings.Settings.AllKeys.Contains(key);
};
SettingsHelper.SetAccessorMethod(accessor);
SettingsHelper.SetDetectorMethod(detector);
// Use the helper to retrieve and set settings
SettingsHelper.SetApplicationSetting&lt;int&gt;("MySettingKey", 42);
int newSetting = SettingsHelper.GetApplicationSetting&lt;int&gt;("MySettingKey");
Console.WriteLine("New setting: {0}", newSetting);
int mySetting = SettingsHelper.GetApplicationSetting&lt;int&gt;("MySettingKey");
Console.WriteLine("Retrieved setting: {0}", mySetting);
}</code></pre>
## Remarks
The specific means of accessing and storing the settings are determined by the consumer,
allowing for flexibility in various environments such as <code>App.config</code> or <code>Web.config</code>
.
## Methods
### <a id="CapyKit_Helpers_SettingsHelper_GetApplicationSetting__1_System_String_"></a> GetApplicationSetting<T\>\(string\)
Retrieves a setting with the given <code>key</code>.
```csharp
public static T GetApplicationSetting<T>(string settingName)
```
#### Parameters
`settingName` [string](https://learn.microsoft.com/dotnet/api/system.string)
The name of the setting to retrieve.
#### Returns
T
The value of the setting as an uncast <code class="typeparamref">T</code>.
#### Type Parameters
`T`
The type of the setting to be retrieved.
### <a id="CapyKit_Helpers_SettingsHelper_SetAccessorMethod_System_Func_System_String_System_Object__"></a> SetAccessorMethod\(Func<string, object\>\)
Sets the function used to retrieve application settings.
```csharp
public static void SetAccessorMethod(Func<string, object> accessor)
```
#### Parameters
`accessor` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<[string](https://learn.microsoft.com/dotnet/api/system.string), [object](https://learn.microsoft.com/dotnet/api/system.object)\>
The new function used to retrieve application settings.
#### Exceptions
[ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)
Thrown when one or more required arguments are null.
### <a id="CapyKit_Helpers_SettingsHelper_SetDetectorMethod_System_Func_System_String_System_Boolean__"></a> SetDetectorMethod\(Func<string, bool\>\)
Sets the function used to detect if an application setting with a given <code>key</code> exists.
```csharp
public static void SetDetectorMethod(Func<string, bool> detector)
```
#### Parameters
`detector` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<[string](https://learn.microsoft.com/dotnet/api/system.string), [bool](https://learn.microsoft.com/dotnet/api/system.boolean)\>
The new function used to detect if an application setting exists.
#### Exceptions
[ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)
Thrown when one or more required arguments are null.