<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FAttribute%2FAttributes_Reflection</id>
		<title>Csharp/CSharp Tutorial/Attribute/Attributes Reflection - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FAttribute%2FAttributes_Reflection"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Attribute/Attributes_Reflection&amp;action=history"/>
		<updated>2026-04-29T23:17:37Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Attribute/Attributes_Reflection&amp;diff=5159&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Attribute/Attributes_Reflection&amp;diff=5159&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Attribute/Attributes_Reflection&amp;diff=5160&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Attribute/Attributes_Reflection&amp;diff=5160&amp;oldid=prev"/>
				<updated>2010-05-26T12:14:30Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Load class method by Attribute==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
[AttributeUsage(AttributeTargets.Class)]&lt;br /&gt;
public class ClassTarget : Attribute&lt;br /&gt;
{&lt;br /&gt;
  public ClassTarget()&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
[AttributeUsage(AttributeTargets.Method )]&lt;br /&gt;
public class MethodTarget : Attribute&lt;br /&gt;
{&lt;br /&gt;
  public MethodTarget()&lt;br /&gt;
  {&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class MyClass&lt;br /&gt;
{&lt;br /&gt;
  [MethodTarget]&lt;br /&gt;
  public int MyMethod()&lt;br /&gt;
  {&lt;br /&gt;
    return 5;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class MainClass &lt;br /&gt;
{&lt;br /&gt;
  public static void Main(string[] args) &lt;br /&gt;
  {&lt;br /&gt;
    ClassTarget rs;&lt;br /&gt;
    MethodTarget rm;&lt;br /&gt;
    Assembly a = Assembly.LoadFrom(&amp;quot;MyClass&amp;quot;);&lt;br /&gt;
    foreach(Type t in a.GetTypes())&lt;br /&gt;
    {&lt;br /&gt;
      rs = (ClassTarget) Attribute.GetCustomAttribute(t, typeof(ClassTarget));&lt;br /&gt;
      if(rs != null)&lt;br /&gt;
      {&lt;br /&gt;
        foreach(MethodInfo m in t.GetMethods())&lt;br /&gt;
        {&lt;br /&gt;
          rm = (MethodTarget) Attribute.GetCustomAttribute(m, typeof(MethodTarget));&lt;br /&gt;
          if(rm != null)&lt;br /&gt;
          {&lt;br /&gt;
            Object o = Activator.CreateInstance(t);&lt;br /&gt;
            Object[] aa = new Object[0];&lt;br /&gt;
            int i = (int) m.Invoke(o, aa);&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reflecting on Attributes==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;//Code revised from &lt;br /&gt;
//A Programmer&amp;quot;s Introduction to C# 2.0, Third Edition&lt;br /&gt;
using System;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
[AttributeUsage(AttributeTargets.Class, AllowMultiple=true)]&lt;br /&gt;
public class CodeReviewAttribute: System.Attribute&lt;br /&gt;
{&lt;br /&gt;
    public CodeReviewAttribute(string reviewer, string date)&lt;br /&gt;
    {&lt;br /&gt;
        this.reviewer = reviewer;&lt;br /&gt;
        this.date = date;&lt;br /&gt;
    }&lt;br /&gt;
    public string Comment&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return(comment);&lt;br /&gt;
        }&lt;br /&gt;
        set&lt;br /&gt;
        {&lt;br /&gt;
            comment = value;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public string Date&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return(date);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public string Reviewer&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            return(reviewer);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    string reviewer;&lt;br /&gt;
    string date;&lt;br /&gt;
    string comment;&lt;br /&gt;
}&lt;br /&gt;
[CodeReview(&amp;quot;Name1&amp;quot;, &amp;quot;01-12-2000&amp;quot;, Comment=&amp;quot;comment1&amp;quot;)]&lt;br /&gt;
[CodeReview(&amp;quot;Name2&amp;quot;, &amp;quot;01-01-2012&amp;quot;, Comment=&amp;quot;comment2&amp;quot;)]&lt;br /&gt;
class Complex&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        Type type = typeof(Complex);&lt;br /&gt;
        foreach (CodeReviewAttribute att in&lt;br /&gt;
        type.GetCustomAttributes(typeof(CodeReviewAttribute), false))&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Reviewer: {0}&amp;quot;, att.Reviewer);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Date: {0}&amp;quot;, att.Date);&lt;br /&gt;
            Console.WriteLine(&amp;quot;Comment: {0}&amp;quot;, att.rument);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Reviewer: Name2&lt;br /&gt;
Date: 01-01-2012&lt;br /&gt;
Comment: comment2&lt;br /&gt;
Reviewer: Name1&lt;br /&gt;
Date: 01-12-2000&lt;br /&gt;
Comment: comment1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Retrieve Attribute by using reflection==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;  &lt;br /&gt;
using System.Reflection; &lt;br /&gt;
  &lt;br /&gt;
[AttributeUsage(AttributeTargets.All)] &lt;br /&gt;
public class MyAttribute : Attribute { &lt;br /&gt;
  public string remark; &lt;br /&gt;
 &lt;br /&gt;
  public MyAttribute(string comment) { &lt;br /&gt;
    remark = comment; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  public string Remark { &lt;br /&gt;
    get { &lt;br /&gt;
      return remark; &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}  &lt;br /&gt;
 &lt;br /&gt;
[MyAttribute(&amp;quot;This class uses an attribute.&amp;quot;)] &lt;br /&gt;
class UseAttrib { &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
class MainClass {  &lt;br /&gt;
  public static void Main() {  &lt;br /&gt;
    Type t = typeof(UseAttrib); &lt;br /&gt;
 &lt;br /&gt;
    Console.Write(&amp;quot;Attributes in &amp;quot; + t.Name + &amp;quot;: &amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
    object[] attribs = t.GetCustomAttributes(false);  &lt;br /&gt;
    foreach(object o in attribs) { &lt;br /&gt;
      Console.WriteLine(o); &lt;br /&gt;
    } &lt;br /&gt;
 &lt;br /&gt;
    Console.Write(&amp;quot;Remark: &amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
    // Retrieve the MyAttribute. &lt;br /&gt;
    Type tRemAtt = typeof(MyAttribute); &lt;br /&gt;
    MyAttribute ra = (MyAttribute) &lt;br /&gt;
          Attribute.GetCustomAttribute(t, tRemAtt); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(ra.remark); &lt;br /&gt;
  }  &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Attributes in UseAttrib: MyAttribute&lt;br /&gt;
Remark: This class uses an attribute.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use Reflection to get the Attribute==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
[assembly:System.CLSCompliantAttribute(true)]&lt;br /&gt;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]&lt;br /&gt;
public class MyDescriptionAttribute : System.Attribute&lt;br /&gt;
{&lt;br /&gt;
  private string description;&lt;br /&gt;
  public string Desc&lt;br /&gt;
  {&lt;br /&gt;
    get { return description; }&lt;br /&gt;
    set { description = value; }&lt;br /&gt;
  }&lt;br /&gt;
  public MyDescriptionAttribute() {}&lt;br /&gt;
  public MyDescriptionAttribute(string desc) &lt;br /&gt;
  { description = desc;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[MyDescriptionAttribute(&amp;quot;Info&amp;quot;)]&lt;br /&gt;
public class MyClass&lt;br /&gt;
{&lt;br /&gt;
    public MyClass()&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static int Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    Type t = typeof(MyClass);&lt;br /&gt;
  &lt;br /&gt;
    // Get all attributes in the assembly.&lt;br /&gt;
    object[] customAtts = t.GetCustomAttributes(false);&lt;br /&gt;
  &lt;br /&gt;
    // List all info.&lt;br /&gt;
    Console.WriteLine(&amp;quot;Value of MyDescriptionAttribute&amp;quot;);&lt;br /&gt;
    foreach(MyDescriptionAttribute v in customAtts)&lt;br /&gt;
      Console.WriteLine(&amp;quot;-&amp;gt; {0}\n&amp;quot;, v.Desc);  &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Value of MyDescriptionAttribute&lt;br /&gt;
-&amp;gt; Info&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use the GetCustomAttributes method==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
public class TrueFalseAttribute : Attribute&lt;br /&gt;
{&lt;br /&gt;
  bool bWritten;&lt;br /&gt;
  public bool Written()&lt;br /&gt;
  {&lt;br /&gt;
    return bWritten;&lt;br /&gt;
  }&lt;br /&gt;
  public TrueFalseAttribute(bool Written)&lt;br /&gt;
  {&lt;br /&gt;
    bWritten = Written;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class StringAttribute : Attribute&lt;br /&gt;
{&lt;br /&gt;
  string sStage;&lt;br /&gt;
  public string Stage()&lt;br /&gt;
  {&lt;br /&gt;
    return sStage;&lt;br /&gt;
  }&lt;br /&gt;
  public StringAttribute(string Stage)&lt;br /&gt;
  {&lt;br /&gt;
    sStage = Stage;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
[TrueFalseAttribute(true)]&lt;br /&gt;
[StringAttribute(&amp;quot;Coding&amp;quot;)]&lt;br /&gt;
public class Class1&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main() &lt;br /&gt;
  {&lt;br /&gt;
    Console.WriteLine(&amp;quot;Class1 attributes: &amp;quot;);object[] aAttributes = Attribute.GetCustomAttributes(typeof(Class1));&lt;br /&gt;
    foreach (object attr in aAttributes)&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(attr);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Class1 attributes:&lt;br /&gt;
StringAttribute&lt;br /&gt;
TrueFalseAttribute&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>