Csharp/C Sharp by API/System/Attribute
Содержание
Attribute.GetCustomAttribute
<source lang="csharp"> using System; using System.Reflection;
[AttributeUsage(AttributeTargets.All)] class RemarkAttribute : Attribute {
string remarkValue; // underlies remark property public string supplement; // this is a named parameter public RemarkAttribute(string comment) { remarkValue = comment; supplement = "None"; } public string remark { get { return remarkValue; } }
}
[RemarkAttribute("This class uses an attribute.",
supplement = "This is additional info.")]
class UseAttrib {
// ...
}
public class NamedParamDemo {
public static void Main() { Type t = typeof(UseAttrib); Console.Write("Attributes in " + t.Name + ": "); object[] attribs = t.GetCustomAttributes(false); foreach(object o in attribs) { Console.WriteLine(o); } // Retrieve the RemarkAttribute. Type tRemAtt = typeof(RemarkAttribute); RemarkAttribute ra = (RemarkAttribute) Attribute.GetCustomAttribute(t, tRemAtt); Console.Write("Remark: "); Console.WriteLine(ra.remark); Console.Write("Supplement: "); Console.WriteLine(ra.supplement); }
}
</source>
Attribute.GetCustomAttribute(Type t, Type tR);
<source lang="csharp"> using System; using System.Reflection;
[AttributeUsage(AttributeTargets.All)] public class MyAttribute : Attribute {
public string remark; public string supplement; public MyAttribute(string comment) { remark = comment; supplement = "None"; } public string Remark { get { return remark; } }
}
[MyAttribute("This class uses an attribute.",
supplement = "This is additional info.")]
class UseAttrib { }
class MainClass {
public static void Main() { Type t = typeof(UseAttrib); Console.Write("Attributes in " + t.Name + ": "); object[] attribs = t.GetCustomAttributes(false); foreach(object o in attribs) { Console.WriteLine(o); } // Retrieve the MyAttribute. Type tRemAtt = typeof(MyAttribute); MyAttribute ra = (MyAttribute) Attribute.GetCustomAttribute(t, tRemAtt); Console.Write("Remark: "); Console.WriteLine(ra.remark); Console.Write("Supplement: "); Console.WriteLine(ra.supplement); }
}
</source>
Attribute.Remove()
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {
public static void Main() { XElement firstParticipant; XDocument xDocument = new XDocument( new XElement("Books", firstParticipant = new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); Console.WriteLine(xDocument); firstParticipant.Attribute("type").Remove(); Console.WriteLine(xDocument); }
}
</source>
Attribute.Value
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass {
public static void Main() { XElement firstParticipant; XDocument xDocument = new XDocument( new XElement("Books", firstParticipant = new XElement("Book", new XAttribute("type", "Author"), new XAttribute("experience", "first-time"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); Console.WriteLine(xDocument); firstParticipant.Attribute("experience").Value = "beginner"; Console.WriteLine(xDocument); }
}
</source>
extends Attribute
<source lang="csharp"> using System;
public class MainClass {
public static void Main() { UnitTest u; Console.Write("Class1 UnitTest attribute: "); u = (UnitTest) Attribute.GetCustomAttribute(typeof(Class1), typeof(UnitTest)); Console.WriteLine(u.Written()); Console.Write("Class2 UnitTest attribute: "); u = (UnitTest) Attribute.GetCustomAttribute(typeof(Class2), typeof(UnitTest)); Console.WriteLine(u.Written()); }
}
public class UnitTest : Attribute {
bool bWritten; public bool Written() { return bWritten; } public UnitTest(bool Written) { bWritten = Written; }
} // apply the UnitTest attribute to two classes [UnitTest(true)] public class Class1 { } [UnitTest(false)] public class Class2 { }
</source>