GetCustomAttributes

using System;
 
namespace QueryAttribs
{
     public enum RemoteServers
     {
         JEANVALJEAN,
         JAVERT,
         COSETTE 
     }
 
     public class RemoteObjectAttribute : Attribute
     {
         public RemoteObjectAttribute(RemoteServers Server)
         {
              this.server = Server;
         }
 
         protected RemoteServers server;
         public string Server
         {
              get
              {
                  return RemoteServers.GetName(
                       typeof(RemoteServers), this.server);
              }
         }
     }
 
     [RemoteObject(RemoteServers.COSETTE)]
     class MyRemotableClass
     {
     }
     class Test
     {
         [STAThread]
         static void Main(string[] args)
         {
              Type type = typeof(MyRemotableClass);
              foreach (Attribute attr in
                  type.GetCustomAttributes(true))
              {
                  RemoteObjectAttribute remoteAttr =
                       attr as RemoteObjectAttribute;
                  if (null != remoteAttr)
                  {
                  Console.WriteLine(
                           "Create this object on {0}.",
                           remoteAttr.Server);
                  }
              }
 
              Console.ReadLine();
         }
     }
}

你可能感兴趣的:(C#)