當前位置: 首頁>>代碼示例>>C#>>正文


C# LogicalMethodInfo.CustomAttributeProvider屬性代碼示例

本文整理匯總了C#中System.Web.Services.Protocols.LogicalMethodInfo.CustomAttributeProvider屬性的典型用法代碼示例。如果您正苦於以下問題:C# LogicalMethodInfo.CustomAttributeProvider屬性的具體用法?C# LogicalMethodInfo.CustomAttributeProvider怎麽用?C# LogicalMethodInfo.CustomAttributeProvider使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。


在下文中一共展示了LogicalMethodInfo.CustomAttributeProvider屬性的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: MyAttribute

//引入命名空間
using System;
using System.Reflection;
using System.Web.Services.Protocols;

// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.ReturnValue, AllowMultiple=true)]
public class MyAttribute : Attribute
{
   private string myName;
   public MyAttribute(string name)
   {
      myName = name;
   }
   public string Name 
   {
      get 
      {
         return myName;
      }
   }
}

public class MyService 
{
   [MyAttribute("This is the first sample attribute")]
   [MyAttribute("This is the second sample attribute")]
   [return: MyAttribute("This is the return sample attribute")]
   public int Add(int xValue, int yValue)
   {
      return (xValue + yValue);
   }
}

public class LogicalMethodInfo_GetCustomAttribute
{
   public static void Main()
   {
      Type myType = typeof(MyService);
      MethodInfo myMethodInfo = myType.GetMethod("Add");
      // Create a synchronous 'LogicalMethodInfo' instance.
      LogicalMethodInfo myLogicalMethodInfo = 
         (LogicalMethodInfo.Create(new MethodInfo[] {myMethodInfo}, 
                                   LogicalMethodTypes.Sync))[0];
      // Display the method for which the attributes are being displayed.
      Console.WriteLine("\nDisplaying the attributes for the method : {0}\n",
                           myLogicalMethodInfo.MethodInfo);

      // Displaying a custom attribute of type 'MyAttribute'
      Console.WriteLine("\nDisplaying attribute of type 'MyAttribute'\n");
      object attribute = myLogicalMethodInfo.GetCustomAttribute(typeof(MyAttribute));
      Console.WriteLine(((MyAttribute)attribute).Name);

      // Display all custom attribute of type 'MyAttribute'.
      Console.WriteLine("\nDisplaying all attributes of type 'MyAttribute'\n");
      object[] attributes = myLogicalMethodInfo.GetCustomAttributes(typeof(MyAttribute));
      for(int i = 0; i < attributes.Length; i++)
         Console.WriteLine(((MyAttribute)attributes[i]).Name);

      // Display all return attributes of type 'MyAttribute'.
      Console.WriteLine("\nDisplaying all return attributes of type 'MyAttribute'\n");
      ICustomAttributeProvider myCustomAttributeProvider = 
                  myLogicalMethodInfo.ReturnTypeCustomAttributeProvider;
      if(myCustomAttributeProvider.IsDefined(typeof(MyAttribute), true))
      {
         attributes = myCustomAttributeProvider.GetCustomAttributes(true);
         for(int i = 0; i < attributes.Length; i++)
            if(attributes[i].GetType().Equals(typeof(MyAttribute)))
               Console.WriteLine(((MyAttribute)attributes[i]).Name);
      }

      // Display all the custom attributes of type 'MyAttribute'.
      Console.WriteLine("\nDisplaying all attributes of type 'MyAttribute'\n");
      myCustomAttributeProvider = myLogicalMethodInfo.CustomAttributeProvider;
      if(myCustomAttributeProvider.IsDefined(typeof(MyAttribute), true))
      {
         attributes = myCustomAttributeProvider.GetCustomAttributes(true);
         for(int i = 0; i < attributes.Length; i++)
            if(attributes[i].GetType().Equals(typeof(MyAttribute)))
               Console.WriteLine(((MyAttribute)attributes[i]).Name);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System.Web.Services.Protocols,代碼行數:83,代碼來源:LogicalMethodInfo.CustomAttributeProvider


注:本文中的System.Web.Services.Protocols.LogicalMethodInfo.CustomAttributeProvider屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。