// Use a named attribute parameter. 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); } }
Author: coder
A simple attribute example
using System; using System.Reflection; [AttributeUsage(AttributeTargets.All)] class RemarkAttribute : Attribute { string remarkValue; public RemarkAttribute(string comment) { remarkValue = comment; } public string remark { get { return remarkValue; } } } [RemarkAttribute("This class uses an attribute.")] class UseAttrib { // ... } public class AttribDemo { 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); } Console.Write("Remark: "); // Retrieve the RemarkAttribute. Type tRemAtt = typeof(RemarkAttribute); RemarkAttribute ra = (RemarkAttribute) Attribute.GetCustomAttribute(t, tRemAtt); Console.WriteLine(ra.remark); } }
Subclass System.Attribute
using System; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] public class AuthorAttribute : System.Attribute { private string company; private string name; public AuthorAttribute(string name) { this.name = name; company = ""; } public string Company { get { return company; } set { company = value; } } public string Name { get { return name; } } } [assembly: Author("Tom", Company = "Ltd.")] [Author("Tom", Company = "Abc Ltd.")] class SomeClass { } [Author("Lena")] public class SomeOtherClass { } [Author("FirstName")] [Author("Jack", Company = "Ltd.")] class MainClass { public static void Main() { Type type = typeof(MainClass); object[] attrs = type.GetCustomAttributes(typeof(AuthorAttribute), true); foreach (AuthorAttribute a in attrs) { Console.WriteLine(a.Name + ", " + a.Company); } } }
The @ prefix turns off the processing of escape characters.
using System; using System.Text; class StringApp { static void Main(string[] args) { string finalString = @" String file: 'C:MyAppStrings'"; Console.WriteLine(finalString); } }
White space is preserved with verbatim strings.
using System; using System.Text; class StringApp { static void Main(string[] args) { string myLongString = @"This is a very very very long string"; Console.WriteLine(myLongString); } }
Demonstrate an @ identier
// Demonstrate an @ identier.
using System;
public class IdTest {
static void Main() {
int @if; // use if as an identifier
for(@if = 0; @if < 10; @if++) Console.WriteLine("@if is " + @if); } } [/csharp]
Using the as Keyword to Work with an Interface
using System; public interface IPrintMessage { void Print(); }; class Class1 { public void Print() { Console.WriteLine("Hello from Class1!"); } } class Class2 : IPrintMessage { public void Print() { Console.WriteLine("Hello from Class2!"); } } class MainClass { public static void Main() { PrintClass PrintObject = new PrintClass(); PrintObject.PrintMessages(); } } class PrintClass { public void PrintMessages() { Class1 Object1 = new Class1(); Class2 Object2 = new Class2(); PrintMessageFromObject(Object1); PrintMessageFromObject(Object2); } private void PrintMessageFromObject(object obj) { IPrintMessage PrintMessage; PrintMessage = obj as IPrintMessage; if(PrintMessage != null) PrintMessage.Print(); } }