当前位置: 首页>>代码示例>>C#>>正文


C# IConfiguration.GetAttribute方法代码示例

本文整理汇总了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;
 }
开发者ID:bage,项目名称:Demo-Logging-Wrapper,代码行数:21,代码来源:ConfigurationAPIObjectFactory.cs

示例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);
            }
        }
开发者ID:bage,项目名称:Demo-Logging-Wrapper,代码行数:60,代码来源:Helper.cs

示例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;
        }
开发者ID:bage,项目名称:Demo-Logging-Wrapper,代码行数:39,代码来源:ConfigurationAPIObjectFactory.cs


注:本文中的IConfiguration.GetAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。