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


C# HtmlElement.TryGetAttribute方法代码示例

本文整理汇总了C#中HtmlElement.TryGetAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlElement.TryGetAttribute方法的具体用法?C# HtmlElement.TryGetAttribute怎么用?C# HtmlElement.TryGetAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HtmlElement的用法示例。


在下文中一共展示了HtmlElement.TryGetAttribute方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VerifyElement

        private static void VerifyElement(HtmlElement element, string tag, bool isVoid, params string[] attributeNames)
        {
            Assert.Equal(tag, element.Tag);
            Assert.Equal(isVoid, element.IsVoid);
            Assert.Null(element.Parent);

            foreach (string attributeName in attributeNames)
            {
                bool attributeIsVoid = typeof(Attribute).GetMethod(attributeName) == null;
                MethodInfo method = element.GetType().GetMethod("With" + attributeName);
                if (attributeIsVoid)
                {
                    HtmlAttribute expectedAttribute = (HtmlAttribute)typeof(Attribute).GetProperty(attributeName).GetValue(null);   

                    Assert.Empty(method.GetParameters());
                    Assert.Same(element, method.Invoke(element, new object[0]));

                    HtmlAttribute actualAttribute;
                    Assert.True(element.TryGetAttribute(expectedAttribute.Name, out actualAttribute));
                    Assert.Equal(expectedAttribute, actualAttribute);
                }
                else
                {
                    HtmlAttribute expectedAttribute = (HtmlAttribute)typeof(Attribute).GetMethod(attributeName).Invoke(null, new object[] { "value" });

                    Assert.Equal(new Type[] { typeof(string) }, method.GetParameters().Select(parameter => parameter.ParameterType));
                    Assert.Same(element, method.Invoke(element, new object[] { "value" }));

                    HtmlAttribute actualAttribute;
                    Assert.True(element.TryGetAttribute(expectedAttribute.Name, out actualAttribute));
                    Assert.Equal(expectedAttribute, actualAttribute);
                }
            }
        }
开发者ID:hughbe,项目名称:html-generator,代码行数:34,代码来源:TagTests.cs

示例2: TryGetAttribute_NullName_ThrowsArgumentNullException

 public void TryGetAttribute_NullName_ThrowsArgumentNullException()
 {
     HtmlElement element = new HtmlElement("element");
     HtmlAttribute attribute = null;
     Assert.Throws<ArgumentNullException>("name", () => element.TryGetAttribute(null, out attribute));
     Assert.Null(attribute);
 }
开发者ID:hughbe,项目名称:html-generator,代码行数:7,代码来源:HtmlElement.ReadTests.cs

示例3: TryGetAttribute_InvalidName_ThrowsArgumentException

 public void TryGetAttribute_InvalidName_ThrowsArgumentException(string name)
 {
     HtmlElement element = new HtmlElement("element");
     HtmlAttribute attribute = null;
     Assert.Throws<ArgumentException>("name", () => element.TryGetAttribute(name, out attribute));
     Assert.Null(attribute);
 }
开发者ID:hughbe,项目名称:html-generator,代码行数:7,代码来源:HtmlElement.ReadTests.cs

示例4: TryGetAttribute

        public void TryGetAttribute(HtmlAttribute[] attributes, string name, HtmlAttribute expected)
        {
            HtmlElement parent = new HtmlElement("element", attributes);

            HtmlAttribute attribute;
            Assert.Equal(expected != null, parent.TryGetAttribute(name, out attribute));
            Assert.Equal(expected, attribute);

            Assert.Equal(expected != null, parent.HasAttribute(name));
        }
开发者ID:hughbe,项目名称:html-generator,代码行数:10,代码来源:HtmlElement.ReadTests.cs


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