c# 自定义特性
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(); } } } (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.rar
(预估有个21文件)
TestConsole
TestConsole.suo
48KB
TestConsole
Program.cs
2KB
packages.config
429B
obj
x86
Debug
DesignTimeResolveAssemblyReferencesInput.cache
6KB
ResolveAssemblyReference.cache
8KB
暂无评论