class MainClass { public static void Main() { System.Reflection.MemberInfo info = typeof(MyClass); object[] attributes = info.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i ++) { System.Console.WriteLine(attributes[i]); } } } 示例 下面是集合所有部分的完整示例。// AttributesTutorial.cs // This example shows the use of class and method attributes.using System; using System.Reflection; using System.Collections;// The IsTested class is a user-defined custom attribute class. // It can be applied to any declaration including // - types (struct, class, enum, delegate) // - members (methods, fields, events, properties, indexers) // It is used with no arguments. public class IsTestedAttribute : Attribute { public override string ToString() { return "Is Tested"; } }// The AuthorAttribute class is a user-defined attribute class. // It can be applied to classes and struct declarations only. // It takes one unnamed string argument (the author's name). // It has one optional named argument Version, which is of type int. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class AuthorAttribute : Attribute { // This constructor specifies the unnamed arguments to the attribute class. public AuthorAttribute(string name) { this.name = name; this.version = 0; } // This property is readonly (it has no set accessor) // so it cannot be used as a named argument to this attribute. public string Name { get { return name; } } // This property is read-write (it has a set accessor) // so it can be used as a named argument when using this // class as an attribute class. public int Version { get { return version; } set { version = value; } } public override string ToString() { string value = "Author : " + Name; if (version != 0) { value += " Version : " + Version.ToString(); } return value; } private string name; private int version; }// Here you attach the AuthorAttribute user-defined custom attribute to // the Account class. The unnamed string argument is passed to the // AuthorAttribute class's constructor when creating the attributes. [Author("Joe Programmer")] class Account { // Attach the IsTestedAttribute custom attribute to this method. [IsTested] public void AddOrder(Order orderToAdd) { orders.Add(orderToAdd); } private ArrayList orders = new ArrayList(); }// Attach the AuthorAttribute and IsTestedAttribute custom attributes // to this class. // Note the use of the 'Version' named argument to the AuthorAttribute. [Author("Jane Programmer", Version = 2), IsTested()] class Order { // add stuff here ... }class MainClass { private static bool IsMemberTested(MemberInfo member) { foreach (object attribute in member.GetCustomAttributes(true)) { if (attribute is IsTestedAttribute) { return true; } } return false; } private static void DumpAttributes(MemberInfo member) { Console.WriteLine("Attributes for : " + member.Name); foreach (object attribute in member.GetCustomAttributes(true)) { Console.WriteLine(attribute); } } public static void Main() { // display attributes for Account class DumpAttributes(typeof(Account)); // display list of tested members foreach (MethodInfo method in (typeof(Account)).GetMethods()) { if (IsMemberTested(method)) { Console.WriteLine("Member {0} is tested!", method.Name); } else { Console.WriteLine("Member {0} is NOT tested!", method.Name); } } Console.WriteLine(); // display attributes for Order class DumpAttributes(typeof(Order)); // display attributes for methods on the Order class foreach (MethodInfo method in (typeof(Order)).GetMethods()) { if (IsMemberTested(method)) { Console.WriteLine("Member {0} is tested!", method.Name); } else { Console.WriteLine("Member {0} is NOT tested!", method.Name); } } Console.WriteLine(); } } 输出 Attributes for : Account Author : Joe Programmer Member GetHashCode is NOT tested! Member Equals is NOT tested! Member ToString is NOT tested! Member AddOrder is tested! Member GetType is NOT tested!Attributes for : Order Author : Jane Programmer Version : 2 Is Tested Member GetHashCode is NOT tested! Member Equals is NOT tested! Member ToString is NOT tested! Member GetType is NOT tested
visable的那个属性是后台的,实际上页面根本没有那个控件
{
this.Visible = true;
}public void Hide()
{
this.Visible = false;
}
以上是用reflector看的源码
{
this.Visible = true;
}public void Hide()
{
this.Visible = false;
}
============================
这个回答强,解决了根本问题。
{
public static void Main()
{
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);
for (int i = 0; i < attributes.Length; i ++)
{
System.Console.WriteLine(attributes[i]);
}
}
}
示例
下面是集合所有部分的完整示例。// AttributesTutorial.cs
// This example shows the use of class and method attributes.using System;
using System.Reflection;
using System.Collections;// The IsTested class is a user-defined custom attribute class.
// It can be applied to any declaration including
// - types (struct, class, enum, delegate)
// - members (methods, fields, events, properties, indexers)
// It is used with no arguments.
public class IsTestedAttribute : Attribute
{
public override string ToString()
{
return "Is Tested";
}
}// The AuthorAttribute class is a user-defined attribute class.
// It can be applied to classes and struct declarations only.
// It takes one unnamed string argument (the author's name).
// It has one optional named argument Version, which is of type int.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class AuthorAttribute : Attribute
{
// This constructor specifies the unnamed arguments to the attribute class.
public AuthorAttribute(string name)
{
this.name = name;
this.version = 0;
} // This property is readonly (it has no set accessor)
// so it cannot be used as a named argument to this attribute.
public string Name
{
get
{
return name;
}
} // This property is read-write (it has a set accessor)
// so it can be used as a named argument when using this
// class as an attribute class.
public int Version
{
get
{
return version;
}
set
{
version = value;
}
} public override string ToString()
{
string value = "Author : " + Name;
if (version != 0)
{
value += " Version : " + Version.ToString();
}
return value;
} private string name;
private int version;
}// Here you attach the AuthorAttribute user-defined custom attribute to
// the Account class. The unnamed string argument is passed to the
// AuthorAttribute class's constructor when creating the attributes.
[Author("Joe Programmer")]
class Account
{
// Attach the IsTestedAttribute custom attribute to this method.
[IsTested]
public void AddOrder(Order orderToAdd)
{
orders.Add(orderToAdd);
} private ArrayList orders = new ArrayList();
}// Attach the AuthorAttribute and IsTestedAttribute custom attributes
// to this class.
// Note the use of the 'Version' named argument to the AuthorAttribute.
[Author("Jane Programmer", Version = 2), IsTested()]
class Order
{
// add stuff here ...
}class MainClass
{
private static bool IsMemberTested(MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is IsTestedAttribute)
{
return true;
}
}
return false;
} private static void DumpAttributes(MemberInfo member)
{
Console.WriteLine("Attributes for : " + member.Name);
foreach (object attribute in member.GetCustomAttributes(true))
{
Console.WriteLine(attribute);
}
} public static void Main()
{
// display attributes for Account class
DumpAttributes(typeof(Account)); // display list of tested members
foreach (MethodInfo method in (typeof(Account)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine(); // display attributes for Order class
DumpAttributes(typeof(Order)); // display attributes for methods on the Order class
foreach (MethodInfo method in (typeof(Order)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();
}
}
输出
Attributes for : Account
Author : Joe Programmer
Member GetHashCode is NOT tested!
Member Equals is NOT tested!
Member ToString is NOT tested!
Member AddOrder is tested!
Member GetType is NOT tested!Attributes for : Order
Author : Jane Programmer Version : 2
Is Tested
Member GetHashCode is NOT tested!
Member Equals is NOT tested!
Member ToString is NOT tested!
Member GetType is NOT tested