本文整理汇总了C#中IConfiguration.GetSimpleAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# IConfiguration.GetSimpleAttribute方法的具体用法?C# IConfiguration.GetSimpleAttribute怎么用?C# IConfiguration.GetSimpleAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConfiguration
的用法示例。
在下文中一共展示了IConfiguration.GetSimpleAttribute方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetBooleanAttribute
/// <summary>
/// <para>
/// Returns a boolean value of the attribute from <paramref name="config"/>.
/// If value is null - method returns <paramref name="defaultValue"/>.
/// If some errors occurred - ConfigException is thrown.
/// </para>
/// </summary>
/// <param name="config">The configuration object to use.</param>
/// <param name="paramName">The name of the attribute.</param>
/// <param name="defaultValue">The default value returned when attribute is not found.</param>
/// <returns>the boolean value of the <paramref name="paramName"/>, or given default value if the
/// attribute is not found.</returns>
/// <exception cref="ConfigException">
/// If configuration fail or some type casting errors are occurred.
/// </exception>
internal static bool GetBooleanAttribute(IConfiguration config, string paramName, bool defaultValue)
{
try
{
object param = config.GetSimpleAttribute(paramName);
if (param == null)
{
return defaultValue;
}
return Convert.ToBoolean(param);
}
catch (Exception e)
{
// duplicate parameters in the config or other errors
throw new ConfigException("Something wrong with configuration.", e);
}
}
示例2: GetLevelAttribute
/// <summary>
/// <para>
/// Returns a Enum value of the attribute from <paramref name="config"/>.
/// If value is null, method returns <paramref name="defaultValue"/>.
/// If some errors occurred, ConfigException is thrown.
/// </para>
/// </summary>
/// <param name="config">The configuration object to use.</param>
/// <param name="name">The name of the attribute.</param>
/// <param name="defaultValue">The default value returned when attribute is not found.</param>
/// <returns>the Enum value of the <paramref name="name"/>, or given default value if the
/// attribute is not found.</returns>
/// <exception cref="ConfigException">
/// If configuration fail or some type casting errors are occurred.
/// </exception>
internal static Level GetLevelAttribute(IConfiguration config, string name, Level defaultValue)
{
try
{
string value = config.GetSimpleAttribute(name) as string;
if (value == null)
{
return defaultValue;
}
return (Level) Enum.Parse(typeof (Level), value, true);
}
catch (Exception e)
{
// duplicate attributes in the config or other errors
throw new ConfigException("Something wrong with configuration.", e);
}
}
示例3: InitializeZeroConfiguration
/// <summary>
/// <para>
/// Initializes the diagnostic backend for the zero configuration logging option.
/// </para>
/// <para>
/// System.Diagnostic output does not go to log files, nor is there the same level of control
/// (rolling logs, etc) as offered by log4NET. So all zero-configuration options are treated the
/// same by this class, and no setup of System.Diagnostics is performed.
/// </para>
/// <para>
/// The one action we do take is if the "source" property is not in the configuration, we put the
/// value "TopCoder Logger" as the value of this property into the configuration.
/// </para>
/// <para>
/// New in 3.0.
/// </para>
/// </summary>
/// <param name="option">The option about which zero-configuration setup should be configured in
/// the backend.</param>
/// <param name="configuration">The configuration object to which needed configuration settings are
/// added.</param>
/// <exception cref="ArgumentNullException">If configuration is null.</exception>
/// <exception cref="ConfigException">
/// If any error occurs when accessing the configuration.
/// </exception>
public static void InitializeZeroConfiguration(ZeroConfigurationOption option,
IConfiguration configuration)
{
Helper.ValidateNotNull(configuration, "configuration");
try
{
if (configuration.GetSimpleAttribute(SOURCE) == null)
{
// set the property to default value in configuration
configuration.SetSimpleAttribute(SOURCE, DEFAULT_SOURCE);
}
}
catch (Exception e)
{
throw new ConfigException("Error occurs when accessing the configuration", e);
}
}
示例4: GetStringAttribute
/// <summary>
/// <para>
/// Returns a string value of the attribute from <paramref name="config"/>.
/// The value should not be null if <paramref name="isRequired"/> is true.
/// The value should not be empty string.
/// If some errors occurred, ConfigException is thrown.
/// </para>
/// </summary>
/// <param name="config">The configuration object to use.</param>
/// <param name="name">The name of the attribute.</param>
/// <param name="isRequired">The flag to indicate whether attribute is required or not.</param>
/// <returns>the string value of the <paramref name="name"/>.</returns>
/// <exception cref="ConfigException">
/// If configuration fail, or it doesn't contain required attribute, or attribute is empty string
/// or some type casting errors are occurred.
/// </exception>
internal static string GetStringAttribute(IConfiguration config, string name, bool isRequired)
{
try
{
string value = config.GetSimpleAttribute(name) as string;
if (isRequired)
{
ValidateNotNull(value, name);
}
ValidateNotEmptyString(value, name);
return value;
}
catch (ArgumentNullException e)
{
throw new ConfigException(
string.Format("Required '{0}' attribute is not set.", e.ParamName), e);
}
catch (ArgumentException e)
{
throw new ConfigException("Invalid value of attribute " + e.ParamName, e);
}
catch (Exception e)
{
// duplicate attributes in the config or another errors
throw new ConfigException("Something wrong with configuration.", e);
}
}
示例5: SimpleLogger
/// <summary>
/// <para>
/// Creates a new instance of the Logger with setting loaded from the given configuration.
/// Delegates for the same named method in super class.
/// </para>
/// <para>
/// If ex is not null, it will be thrown.
/// </para>
/// </summary>
/// <param name="configuration">The configuration object to load settings from.</param>
/// <exception cref="ArgumentNullException">If configuration is null.</exception>
/// <exception cref="ConfigException">
/// If any of the configuration settings are missing or are invalid values.
/// </exception>
public SimpleLogger(IConfiguration configuration)
: base(configuration)
{
if (ex != null)
{
Exception tmpEx = ex;
ex = null;
throw tmpEx;
}
isInitiaized = configuration.GetSimpleAttribute("isInitiaized") != null;
}