【文件属性】:
文件名称:c# 自定义特性
文件大小:74KB
文件格式:RAR
更新时间:2016-01-29 17:01:02
c# 自定义 特性 Attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
namespace TestConsole
{
public class JWAttribute : Attribute
{
public string param1 { get; set; }
public string param2 { get; set; }
public string paramDefault = "paramDefault";
}
[JW(param1 = "JW-A")]
public class A
{
[JW(param2 = "JW-property")]
public string property { get; set; }
[JW(param2 = "JW-fun")]
public void fun() {}
public void nofun() {}
}
class Program
{
static void Main(string[] args)
{
JWAttribute jw;
jw = typeof(A).GetCustomAttributes(typeof(JWAttribute), false).Single() as JWAttribute;
Console.WriteLine(jw.param1);
jw = typeof(A).GetProperty("property").GetCustomAttributes(typeof(JWAttribute), false).Single() as JWAttribute;
Console.WriteLine(jw.param2);
jw = typeof(A).GetMember("fun").Single().GetCustomAttributes(typeof(JWAttribute), false).Single() as JWAttribute;
Console.WriteLine(jw.param2);
Console.WriteLine(typeof(A).GetMember("nofun").Single().GetCustomAttributes(typeof(JWAttribute), false).Count());
Console.WriteLine(jw.paramDefault);
AssemblyCopyrightAttribute aca = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false).Single() as AssemblyCopyrightAttribute;
Console.WriteLine(aca.Copyright);
Console.ReadKey();
}
}
}
【文件预览】:
TestConsole
----TestConsole.suo(48KB)
----TestConsole()
--------Program.cs(2KB)
--------packages.config(429B)
--------obj()
--------bin()
--------oraunls11.dll(104KB)
--------App.Config(419B)
--------TestConsole.csproj(3KB)
--------TestConsole.csproj.user(227B)
--------Properties()
----TestConsole.sln.docstates.suo(1KB)
----TestConsole.sln(875B)