<?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%2FAttribute_Definition</id>
		<title>Csharp/CSharp Tutorial/Attribute/Attribute Definition - История изменений</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%2FAttribute_Definition"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Attribute/Attribute_Definition&amp;action=history"/>
		<updated>2026-04-29T15:10:08Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Attribute/Attribute_Definition&amp;diff=5151&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/Attribute_Definition&amp;diff=5151&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/Attribute_Definition&amp;diff=5152&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/Attribute_Definition&amp;diff=5152&amp;oldid=prev"/>
				<updated>2010-05-26T12:14:29Z</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;==A custom attribute based on bool value==&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;
[TrueFalseAttribute(true)]&lt;br /&gt;
public class Class1&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
[TrueFalseAttribute(false)]&lt;br /&gt;
public class Class2&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;
    TrueFalseAttribute u;&lt;br /&gt;
    Console.Write(&amp;quot;Class1 TrueFalseAttribute attribute: &amp;quot;);&lt;br /&gt;
    u = (TrueFalseAttribute) Attribute.GetCustomAttribute(typeof(Class1), typeof(TrueFalseAttribute));&lt;br /&gt;
    Console.WriteLine(u.Written());&lt;br /&gt;
    Console.Write(&amp;quot;Class2 TrueFalseAttribute attribute: &amp;quot;);&lt;br /&gt;
    u = (TrueFalseAttribute) Attribute.GetCustomAttribute(typeof(Class2), typeof(TrueFalseAttribute));&lt;br /&gt;
    Console.WriteLine(u.Written());&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Class1 TrueFalseAttribute attribute: True&lt;br /&gt;
Class2 TrueFalseAttribute attribute: False&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Assembly level attributes don&amp;quot;t have to be in the assemblyinfo.cs file, but must be lised outside of any namespae definition.==&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;
==Attribute with supplement information==&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;
[AttributeUsage(AttributeTargets.All)] &lt;br /&gt;
public class MyAttribute : Attribute { &lt;br /&gt;
  string remark;&lt;br /&gt;
 &lt;br /&gt;
  public string supplement;&lt;br /&gt;
 &lt;br /&gt;
  public MyAttribute(string comment) { &lt;br /&gt;
    remark = comment; &lt;br /&gt;
    supplement = &amp;quot;None&amp;quot;; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Create 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;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]&lt;br /&gt;
public class AuthorAttribute : System.Attribute&lt;br /&gt;
{&lt;br /&gt;
    public string Company; &lt;br /&gt;
    public string Name;    &lt;br /&gt;
    public AuthorAttribute(string name)&lt;br /&gt;
    {&lt;br /&gt;
        this.Name = name;&lt;br /&gt;
        Company = &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[Author(&amp;quot;Name1&amp;quot;)]&lt;br /&gt;
[Author(&amp;quot;Name2&amp;quot;, Company = &amp;quot;Ltd.&amp;quot;)]&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        Type type = typeof(MainClass);&lt;br /&gt;
        object[] attrs = type.GetCustomAttributes(typeof(AuthorAttribute), true);&lt;br /&gt;
        foreach (AuthorAttribute a in attrs)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(a.Name + &amp;quot;, &amp;quot; + a.rupany);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Name2, Ltd.&lt;br /&gt;
Name1,&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Positional vs. Named Parameters==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;OL&amp;gt;&amp;lt;LI&amp;gt;A positional parameter is linked by its position.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;Positional parameters must be specified in the order in which they appear.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;Named parameters are specified by assigning values to their names.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;For an attribute, you can also create named parameters&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;named parameters can be assigned initial values by using their names.&amp;lt;/LI&amp;gt;&amp;lt;LI&amp;gt;A named parameter is supported by either a public field or property, which must not be read-only.&amp;lt;/LI&amp;gt;&amp;lt;/OL&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Here is the general form of an attribute specification that includes named parameters:&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;[attrib(positional-param-list, named-param1 = value, named-param2 = value, ...)]&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use a named attribute parameter.==&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 string supplement; &lt;br /&gt;
 &lt;br /&gt;
  public MyAttribute(string comment) { &lt;br /&gt;
    remark = comment; &lt;br /&gt;
    supplement = &amp;quot;None&amp;quot;; &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;
                 supplement = &amp;quot;This is additional info.&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;
    // 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.Write(&amp;quot;Remark: &amp;quot;); &lt;br /&gt;
    Console.WriteLine(ra.remark); &lt;br /&gt;
 &lt;br /&gt;
    Console.Write(&amp;quot;Supplement: &amp;quot;); &lt;br /&gt;
    Console.WriteLine(ra.supplement); &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.&lt;br /&gt;
Supplement: This is additional info.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use a property as a named attribute parameter.==&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 int priority; &lt;br /&gt;
 &lt;br /&gt;
  public string supplement; &lt;br /&gt;
 &lt;br /&gt;
  public MyAttribute(string comment) { &lt;br /&gt;
    remark = comment; &lt;br /&gt;
    supplement = &amp;quot;None&amp;quot;; &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;
  public int Priority { &lt;br /&gt;
    get { &lt;br /&gt;
      return priority; &lt;br /&gt;
    } &lt;br /&gt;
    set { &lt;br /&gt;
      priority = value; &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;
                 supplement = &amp;quot;This is additional info.&amp;quot;, &lt;br /&gt;
                 Priority = 10)] &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;
    // 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.Write(&amp;quot;Remark: &amp;quot;); &lt;br /&gt;
    Console.WriteLine(ra.remark); &lt;br /&gt;
 &lt;br /&gt;
    Console.Write(&amp;quot;Supplement: &amp;quot;); &lt;br /&gt;
    Console.WriteLine(ra.supplement); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Priority: &amp;quot; + ra.priority); &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.&lt;br /&gt;
Supplement: This is additional info.&lt;br /&gt;
Priority: 10&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>