本文整理汇总了C#中System.Attribute.IsDefaultAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# Attribute.IsDefaultAttribute方法的具体用法?C# Attribute.IsDefaultAttribute怎么用?C# Attribute.IsDefaultAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Attribute
的用法示例。
在下文中一共展示了Attribute.IsDefaultAttribute方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnimalTypeAttribute
//引入命名空间
using System;
using System.Reflection;
namespace DefAttrCS
{
// An enumeration of animals. Start at 1 (0 = uninitialized).
public enum Animal
{
// Pets.
Dog = 1,
Cat,
Bird,
}
// A custom attribute to allow a target to have a pet.
public class AnimalTypeAttribute : Attribute
{
// The constructor is called when the attribute is set.
public AnimalTypeAttribute(Animal pet)
{
thePet = pet;
}
// Provide a default constructor and make Dog the default.
public AnimalTypeAttribute()
{
thePet = Animal.Dog;
}
// Keep a variable internally ...
protected Animal thePet;
// .. and show a copy to the outside world.
public Animal Pet
{
get { return thePet; }
set { thePet = Pet; }
}
// Override IsDefaultAttribute to return the correct response.
public override bool IsDefaultAttribute()
{
if (thePet == Animal.Dog)
return true;
return false;
}
}
public class TestClass
{
// Use the default constructor.
[AnimalType]
public void Method1()
{}
}
class DemoClass
{
static void Main(string[] args)
{
// Get the class type to access its metadata.
Type clsType = typeof(TestClass);
// Get type information for the method.
MethodInfo mInfo = clsType.GetMethod("Method1");
// Get the AnimalType attribute for the method.
AnimalTypeAttribute atAttr =
(AnimalTypeAttribute)Attribute.GetCustomAttribute(mInfo,
typeof(AnimalTypeAttribute));
// Check to see if the default attribute is applied.
Console.WriteLine("The attribute {0} for method {1} in class {2}",
atAttr.Pet, mInfo.Name, clsType.Name);
Console.WriteLine("{0} the default for the AnimalType attribute.",
atAttr.IsDefaultAttribute() ? "is" : "is not");
}
}
}