Attribute

luishu 2006-03-01 03:07:41
在自定义的attribute类当中是否可以添加自已定义的方法?如果可以的希望能提供一个例子。还有,如何使用Attribute类,在使用用时应注意那些问题?
...全文
60 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
luishu 2006-03-01
  • 打赏
  • 举报
回复
你没有回到我问题的主题。
xujiaoxiang 2006-03-01
  • 打赏
  • 举报
回复
下面的代码实例说明了 Attribute 的用法:

[Visual Basic]
Imports System
Imports System.Reflection

Public Module CustomAttrVB

' An enumeration of animals. Start at 1 (0 = uninitialized).
Public Enum Animal
' Pets
Dog = 1
Cat
Bird
End Enum

' Visual Basic requires the AttributeUsage be specified.
' A custom attribute to allow a target to have a pet.
<AttributeUsage(AttributeTargets.Method)> _
Public Class AnimalTypeAttribute
Inherits Attribute

' The constructor is called when the attribute is set.
Public Sub New(ByVal animal As Animal)
Me.thePet = animal
End Sub

' Keep a variable internally ...
Protected thePet As Animal

' .. and show a copy to the outside world.
Public Property Pet() As Animal
Get
Return thePet
End Get
Set(ByVal Value As Animal)
thePet = Value
End Set
End Property

End Class

' A test class where each method has its own pet.
Class AnimalTypeTestClass

<AnimalType(Animal.Dog)> _
Public Sub DogMethod()
End Sub

<AnimalType(Animal.Cat)> _
Public Sub CatMethod()
End Sub

<AnimalType(Animal.Bird)> _
Public Sub BirdMethod()
End Sub
End Class

' The runtime test.
Sub Main()
Dim testClass As New AnimalTypeTestClass()
Dim tcType As Type = testClass.GetType()
Dim mInfo As MethodInfo
' Iterate through all the methods of the class.
For Each mInfo In tcType.GetMethods()
Dim attr As Attribute
' Iterate through all the attributes of the method.
For Each attr In Attribute.GetCustomAttributes(mInfo)
If TypeOf attr Is AnimalTypeAttribute Then
Dim attrCustom As AnimalTypeAttribute = _
CType(attr, AnimalTypeAttribute)
Console.WriteLine("Method {0} has a pet {1} attribute.", _
mInfo.Name(), attrCustom.Pet.ToString())
End If
Next
Next
End Sub
End Module

' Output:
' Method DogMethod has a pet Dog attribute.
' Method CatMethod has a pet Cat attribute.
' Method BirdMethod has a pet Bird attribute.

[C#]
using System;
using System.Reflection;

namespace CustomAttrCS {
// 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;
}

// Keep a variable internally ...
protected Animal thePet;

// .. and show a copy to the outside world.
public Animal Pet {
get { return thePet; }
set { thePet = Pet; }
}
}

// A test class where each method has its own pet.
class AnimalTypeTestClass {
[AnimalType(Animal.Dog)]
public void DogMethod() {}

[AnimalType(Animal.Cat)]
public void CatMethod() {}

[AnimalType(Animal.Bird)]
public void BirdMethod() {}
}

class DemoClass {
static void Main(string[] args) {
AnimalTypeTestClass testClass = new AnimalTypeTestClass();
Type type = testClass.GetType();
// Iterate through all the methods of the class.
foreach(MethodInfo mInfo in type.GetMethods()) {
// Iterate through all the Attributes for each method.
foreach (Attribute attr in
Attribute.GetCustomAttributes(mInfo)) {
// Check for the AnimalType attribute.
if (attr.GetType() == typeof(AnimalTypeAttribute))
Console.WriteLine(
"Method {0} has a pet {1} attribute.",
mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
}

}
}
}
}

/*
* Output:
* Method DogMethod has a pet Dog attribute.
* Method CatMethod has a pet Cat attribute.
* Method BirdMethod has a pet Bird attribute.
*/

是这个Attribute 吗?
startray 2006-03-01
  • 打赏
  • 举报
回复
this.Button1.Attributes.Add["onclick"]="";

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧