using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AttributeTest
{
[AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
class AttributeInfo:Attribute
{
private string name;
private string type;
private int age;
public AttributeInfo(string name,string type,int age)
{
this.name = name;
this.type = type;
this.age = age;
}
public string Name
{
get
{
return name;
}
}
public string Type
{
get
{
return type;
}
}
public int Age
{
get
{
return age;
}
}
public string message { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace AttributeTest
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
Type info = typeof(testAttr);
PropertyInfo[] ps = info.GetProperties();
Console.WriteLine("遍历自定义类的属性");
object[] attributes = info.GetCustomAttributes(false);
for (int i = 0; i < attributes.Length; i++)
{
AttributeInfo t = (AttributeInfo)attributes[i];
System.Console.WriteLine(t.Name+" "+t.Type+" "+t.message);
}
Console.WriteLine("遍历方法的自定义属性");
var methods=info.GetMethods();
foreach(var method in methods)
{
var mattrs = method.GetCustomAttributes(false);
for (int i = 0; i < mattrs.Length; i++)
{
AttributeInfo t = mattrs[i] as AttributeInfo;
if(t!=null)
{
Console.WriteLine("方法名:" + method.Name);
System.Console.WriteLine(t.Name + " " + t.Type + " " + t.message);
}
}
}
Console.WriteLine("遍历属性的自定义属性");
var pss=info.GetProperties();
foreach(var p in pss)
{
var pattrs=p.GetCustomAttributes(false);
for (int i = 0; i < pattrs.Length; i++)
{
AttributeInfo t = pattrs[i] as AttributeInfo;
if(t!=null)
{
Console.WriteLine("属性名:" + p.Name);
System.Console.WriteLine(t.Name + " " + t.Type + " " + t.message);
}
}
}
Console.ReadKey();
}
}
[AttributeInfo("张三","男",29,message ="测试人员信息")]
[AttributeInfo("李四","女",229,message ="测试人员信息222")]
class testAttr
{
[AttributeInfo("王五","不男不女",33,message ="泰国人妖")]
public int sum(int a,int b)
{
return a + b;
}
[AttributeInfo("乘法", "100", 200, message = "乘法口诀")]
public int cheng(int a, int b)
{
return a + b;
}
[AttributeInfo("属性1", "值1", 1)]
public string lotnum { get; set; }
[AttributeInfo("属性2", "值2", 2)]
public string partnum { get; set; }
}
}
最后附带 相关知识点详细定义
预定义特性(Attribute)
.Net 框架提供了三种预定义特性:
• AttributeUsage
• Conditional
• Obsolete
AttributeUsage
预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。
规定该特性的语法如下:
[AttributeUsage(
validon,
AllowMultiple=allowmultiple,
Inherited=inherited
)]
其中:
• 参数 validon 规定特性可被放置的语言元素。它是枚举器 AttributeTargets 的值的组合。默认值是 AttributeTargets.All。
• 参数 allowmultiple(可选的)为该特性的 AllowMultiple 属性(property)提供一个布尔值。如果为 true,则该特性是多用的。默认值是 false(单用的)。
比如[AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
class AttributeInfo:Attribute
那么[AttributeInfo("张三","男",29,message ="测试人员信息")]
[AttributeInfo("张三2","男",229,message ="测试人员信息")]
class testAttr
就不会报错了
• 参数 inherited(可选的)为该特性的 Inherited 属性(property)提供一个布尔值。如果为 true,则该特性可被派生类继承。默认值是 false(不被继承)。
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
Conditional
这个预定义特性标记了一个条件方法,其执行依赖于指定的预处理标识符。
它会引起方法调用的条件编译,取决于指定的值,比如 Debug 或 Trace。
static void Main(string[] args)
{
DebugTest.alert("inmain"); 只有VS在Debug的时候才会调用这个方法
DebugTest.alert2("inmain2");
Console.ReadKey();
}
[Conditional("DEBUG")]
public static void alert(string msg)
{
Console.WriteLine(msg);
}
Obsolete
[Obsolete(
message, 提示类方法过去的文字提示内容
iserror false,方法还能用.ture 那么编译器就会报错
)]
[Obsolete("方法已过时",false)]//如果是true,那么编译器会报错
public static void alert2(string msg)
{
Console.WriteLine(msg);
}