62,046
社区成员
发帖
与我相关
我的任务
分享
[DefaultEvent("FormClosed")]
public partial class Form1 : Form
public partial class Form2 : Form1
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
//获取类上的属性类
foreach (object obj in typeof(TestClass).GetCustomAttributes(false))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字段属性类
foreach (object obj in typeof(TestClass).GetField("_aa").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取属性属性类
foreach (object obj in typeof(TestClass).GetProperty("aa").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取方法属性类
foreach (object obj in typeof(TestClass).GetMethod("test").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
//获取字事件性类
foreach (object obj in typeof(TestClass).GetEvent("onTest").GetCustomAttributes(typeof(Test1), true))
{
Console.WriteLine(obj.GetType().Name + ":" + obj.ToString());
}
Console.Read();
}
}
//测试属性类,传入string
public class Test1 : System.Attribute
{
string strName = "";
public Test1() { }
public Test1(string str)
{
strName = str;
}
public override string ToString()
{
return strName;
}
}
//测试属性类,传入int
public class Test2 : Attribute
{
int _f;
public Test2(int f) { _f = f; }
public override string ToString()
{
return _f.ToString();
}
}
//给class测试
[Test1("jinjazz for TestClass")]
[Test2(12356)]
public class TestClass
{
//给字段和属性测试
[Test1("jinjazz for _aa字段 ")]
public string _aa = "";
[Test1("jinjazz for aa 属性")]
public string aa
{
get { return _aa; }
}
//给方法和事件测试
[Test1("jinjazz for test 方法")]
public void test() { }
[Test1("jinjazz for onTest 事件")]
public event System.EventHandler onTest;
}
}