本文整理汇总了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);
}
}
}
示例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);
}
示例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);
}
示例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));
}