这个例子我已经贴第3遍了,问问题前应该先CSDN搜索 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection;namespace WindowsFormsApplication14 { public partial class Form1 : Form { public Form1() { InitializeComponent(); foreach (MethodInfo MI in this.GetType().GetMethods()) { bool Special = false; foreach (Attribute Attrib in MI.GetCustomAttributes(true)) if (MI.Name == "A") if (Attrib is XAttribute) { Special = true; break; } if (Special) MessageBox.Show(MI.Name + "方法是特别的X方法"); else if (MI.Name == "B") // 因为其他普通的方法太多,这里就显示B MessageBox.Show(MI.Name + "方法是普通的方法"); } } [X] public void A() { } public void B() { } [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class XAttribute : Attribute { public XAttribute() { } } } }
CSDN的搜索做的不好。显示的是2002年老论坛的东西。
精简了一下,好像解释和没解释差不多,本来就是很简单的几句 public partial class Form1 : Form { public Form1() { InitializeComponent(); foreach (MethodInfo MI in this.GetType().GetMethods()) // 遍历公共方法 if (MI.Name == "A") // 方法名为A() foreach (Attribute Attrib in MI.GetCustomAttributes(true)) // 遍历特性 { if (Attrib is XAttribute) // 拥有自定义属性X { MessageBox.Show(MI.Name + "方法是特别的X方法"); break; } } else MessageBox.Show(MI.Name + "方法是普通的方法"); } [X] // A方法加一个特性 public void A() { } public void B() { } [AttributeUsage(AttributeTargets.Method)] //特性只能应用于方法 public class XAttribute : Attribute { } }
公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。 [DllImport("User32.dll")] public static extern int MessageBox(int hParent, string Message, string Caption, int Type);
区分不同的对象不就是用途么,至于做什么看你想干什么了下面这个就是可以指定哪个类在form初始化时自动产生一个实例并输出AAA using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection;namespace WindowsApplication190 { public partial class Form1 : Form { public Form1() { InitializeComponent(); foreach (Type T in this.GetType().GetNestedTypes()) // 遍历公共方法 if (T.Name == "A") foreach (Attribute Attrib in T.GetCustomAttributes(true)) // 遍历特性 { if (Attrib is XAttribute) // 拥有自定义属性X { String Text = Attrib.GetType().GetProperty("Text").GetValue(Attrib, null).ToString(); T.GetConstructor(new Type[] { typeof(String) }).Invoke(new Object[] { Text }); } } } [X("AAA")] public class A { public A(String S) { MessageBox.Show("A " + S); } } public class B { public B(String S) { MessageBox.Show("B " + S); } } [AttributeUsage(AttributeTargets.Class)] public class XAttribute : Attribute { String _Text = String.Empty; public String Text { get { return _Text; } } public XAttribute(String Text) { this._Text = Text; } } } }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); foreach (MethodInfo MI in this.GetType().GetMethods())
{
bool Special = false; foreach (Attribute Attrib in MI.GetCustomAttributes(true))
if (MI.Name == "A")
if (Attrib is XAttribute)
{
Special = true;
break;
}
if (Special)
MessageBox.Show(MI.Name + "方法是特别的X方法");
else if (MI.Name == "B") // 因为其他普通的方法太多,这里就显示B
MessageBox.Show(MI.Name + "方法是普通的方法");
}
} [X]
public void A()
{
} public void B()
{
} [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class XAttribute : Attribute
{
public XAttribute()
{
}
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); foreach (MethodInfo MI in this.GetType().GetMethods()) // 遍历公共方法
if (MI.Name == "A") // 方法名为A()
foreach (Attribute Attrib in MI.GetCustomAttributes(true)) // 遍历特性
{
if (Attrib is XAttribute) // 拥有自定义属性X
{
MessageBox.Show(MI.Name + "方法是特别的X方法");
break;
}
}
else
MessageBox.Show(MI.Name + "方法是普通的方法");
} [X] // A方法加一个特性
public void A()
{
} public void B()
{
} [AttributeUsage(AttributeTargets.Method)] //特性只能应用于方法
public class XAttribute : Attribute { }
}
[DllImport("User32.dll")]
public static extern int MessageBox(int hParent, string Message, string Caption, int Type);
static void Main(string[] args)
{
DisplayRunningMessage();
DisplayDebugMessage();
MessageBox(0,"Hello","Message",0);
Console.ReadLine();
}
[Conditional("DEBUG")]
private static void DisplayRunningMessage()
{
Console.WriteLine("开始运行Main子程序。当前时间是"+DateTime.Now);
}
[Conditional("DEBUG")]
[Obsolete]
private static void DisplayDebugMessage()
{
Console.WriteLine("开始Main子程序");
}
}
} 如果在一个程序元素前面声明一个Attribute,那么就表示这个Attribute被施加到该元素上,[DllImport]施加到MessageBox函数上, [Conditional]施加到DisplayRuntimeMessage方法和DisplayDebugMessage方法,[Obsolete]施加到DisplayDebugMessage方法上。
public static extern int MessageBox(int hParent, string Message, string Caption, int Type); [Conditional("DEBUG")]//指定此函数正在debug版本运行,release下当成没有。有点代替c++中的#ifdef _DEBUG #else 的意思。
private static void DisplayRunningMessage()
{
Console.WriteLine("开始运行Main子程序。当前时间是" + DateTime.Now);
}
A是个BUTTON
如果你设定属性ENABLE=FALSE,那么你在JS里面用document.getElementID是获取不到它的
但是如果你写成this.a.Attribute.add("style","display:none")
你用JS就可以获取到了
区分不同的对象不就是用途么,至于做什么看你想干什么了下面这个就是可以指定哪个类在form初始化时自动产生一个实例并输出AAA
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;namespace WindowsApplication190
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); foreach (Type T in this.GetType().GetNestedTypes()) // 遍历公共方法
if (T.Name == "A")
foreach (Attribute Attrib in T.GetCustomAttributes(true)) // 遍历特性
{
if (Attrib is XAttribute) // 拥有自定义属性X
{
String Text = Attrib.GetType().GetProperty("Text").GetValue(Attrib, null).ToString();
T.GetConstructor(new Type[] { typeof(String) }).Invoke(new Object[] { Text });
}
}
} [X("AAA")]
public class A
{
public A(String S)
{
MessageBox.Show("A " + S);
}
} public class B
{
public B(String S)
{
MessageBox.Show("B " + S);
}
} [AttributeUsage(AttributeTargets.Class)]
public class XAttribute : Attribute
{
String _Text = String.Empty; public String Text
{
get
{
return _Text;
}
} public XAttribute(String Text)
{
this._Text = Text;
}
}
}
}