本文整理汇总了C#中IConfiguration.GetAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# IConfiguration.GetAttribute方法的具体用法?C# IConfiguration.GetAttribute怎么用?C# IConfiguration.GetAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConfiguration
的用法示例。
在下文中一共展示了IConfiguration.GetAttribute方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsNameMatch
/// <summary>
/// <para>
/// Checks whether the configuration is matched with specified name.
/// </para>
/// </summary>
///
/// <param name="config">
/// The configuration to check.
/// </param>
/// <param name="name">
/// The expected name attribute value.
/// </param>
///
/// <returns>
/// Whether the configuration is matched with specified name.
/// </returns>
private static bool IsNameMatch(IConfiguration config, string name)
{
object[] attr = config.GetAttribute(ConfigName);
return attr != null && attr.Length == 1 && (attr[0] as string) == name;
}
示例2: GetStringListAttribute
/// <summary>
/// <para>
/// Returns a list of string values of the attribute from <paramref name="config"/>.
/// The list should not be null if <paramref name="isRequired"/> is true.
/// The list should not contains empty strings.
/// 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 list of string values of the <paramref name="name"/>.</returns>
/// <exception cref="ConfigException">
/// If configuration fail, or it doesn't contain required attribute, or attribute contains empty
/// strings or some type casting errors are occurred.
/// </exception>
internal static IList<string> GetStringListAttribute(IConfiguration config, string name,
bool isRequired)
{
try
{
object[] values = config.GetAttribute(name);
if (isRequired)
{
ValidateNotNull(values, name);
}
IList<string> list = new List<string>();
if (values != null)
{
foreach (object value in values)
{
string str = value as string;
ValidateNotEmptyString(str, name);
// add to list
list.Add(str);
}
}
return list;
}
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);
}
}
示例3: GetSimpleAttribute
/// <summary>
/// <para>
/// Gets an attribute value from a configuration.
/// </para>
/// </summary>
///
/// <param name="config">
/// The configuration to read.
/// </param>
/// <param name="attrName">
/// The attribute name.
/// </param>
/// <param name="mandatory">
/// Whether the value is mandatory.
/// </param>
///
/// <returns>
/// The value read.
/// </returns>
///
/// <exception cref="ConfigurationAPISourceException">
/// If the value is missing and it is mandatory.
/// </exception>
private static string GetSimpleAttribute(IConfiguration config, string attrName, bool mandatory)
{
object[] values = config.GetAttribute(attrName);
if (values != null && values.Length == 1)
{
return values[0] as string;
}
if (mandatory)
{
throw new ConfigurationAPISourceException(
String.Format("Configuration [{0}] must be a single string.", attrName));
}
return null;
}