次の方法で共有


LogicalMethodInfo.GetCustomAttribute メソッド

カスタム属性がこの型に適用されている場合は、最初に適用されたカスタム属性を返します。

Public Function GetCustomAttribute( _
   ByVal type As Type _) As Object
[C#]
public object GetCustomAttribute(Typetype);
[C++]
public: Object* GetCustomAttribute(Type* type);
[JScript]
public function GetCustomAttribute(
   type : Type) : Object;

パラメータ

  • type
    カスタム属性を適用する対象の Type

戻り値

type パラメータに適用された最初のカスタム属性を格納している Object

例外

例外の種類 条件
TypeLoadException カスタム属性の型を読み込むことができません。

使用例

 
Imports System
Imports System.Reflection
Imports System.Web.Services.Protocols
Imports MicroSoft.VisualBasic

' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.Method Or AttributeTargets.ReturnValue, AllowMultiple := True)>  _
Public Class MyAttribute
   Inherits Attribute

   Private myName As String
   
   Public Sub New(name As String)
      myName = name
   End Sub 'New
   
   Public ReadOnly Property Name() As String
      Get
         Return myName
      End Get
   End Property
End Class 'MyAttribute

Public Class MyService
   
<MyAttribute("This is the first sample attribute"), MyAttribute("This is the second sample attribute")>  _
   Public Function Add(xValue As Integer, yValue As Integer)  _
                 As<MyAttribute("This is the return sample attribute")> Integer
      Return xValue + yValue
   End Function 'Add
End Class 'MyService


Public Class LogicalMethodInfo_GetCustomAttribute
   
   Public Shared Sub Main()
      Dim myType As Type = GetType(MyService)
      Dim myMethodInfo As MethodInfo = myType.GetMethod("Add")
      ' Create a synchronous 'LogicalMethodInfo' instance.
      Dim myLogicalMethodInfo As LogicalMethodInfo = _
                 LogicalMethodInfo.Create(New MethodInfo() {myMethodInfo}, LogicalMethodTypes.Sync)(0)
      ' Display the method for which the attributes are being displayed.
      Console.WriteLine(ControlChars.NewLine + "Displaying the attributes for the method : {0}" + _
                 ControlChars.NewLine, myLogicalMethodInfo.MethodInfo.ToString())
      
      ' Displaying a custom attribute of type 'MyAttribute'
      Console.WriteLine(ControlChars.NewLine + "Displaying attribute of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      Dim attribute As Object = myLogicalMethodInfo.GetCustomAttribute(GetType(MyAttribute))
      Console.WriteLine(CType(attribute, MyAttribute).Name)
      
      ' Display all custom attribute of type 'MyAttribute'.
      Console.WriteLine(ControlChars.NewLine + "Displaying all attributes of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      Dim attributes As Object() = myLogicalMethodInfo.GetCustomAttributes(GetType(MyAttribute))
      Dim i As Integer
      For i = 0 To attributes.Length - 1
         Console.WriteLine(CType(attributes(i), MyAttribute).Name)
      Next i 
      ' Display all return attributes of type 'MyAttribute'.
      Console.WriteLine(ControlChars.NewLine + "Displaying all return attributes of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      Dim myCustomAttributeProvider As ICustomAttributeProvider = _
                 myLogicalMethodInfo.ReturnTypeCustomAttributeProvider
      If myCustomAttributeProvider.IsDefined(GetType(MyAttribute), True) Then
         attributes = myCustomAttributeProvider.GetCustomAttributes(True)

         For i = 0 To attributes.Length - 1
            If attributes(i).GetType().Equals(GetType(MyAttribute)) Then
               Console.WriteLine(CType(attributes(i), MyAttribute).Name)
            End If
         Next i 
      End If ' Display all the custom attributes of type 'MyAttribute'.
      Console.WriteLine(ControlChars.NewLine + "Displaying all attributes of type 'MyAttribute'" + _
                 ControlChars.NewLine)
      myCustomAttributeProvider = myLogicalMethodInfo.CustomAttributeProvider
      If myCustomAttributeProvider.IsDefined(GetType(MyAttribute), True) Then
         attributes = myCustomAttributeProvider.GetCustomAttributes(True)

         For i = 0 To attributes.Length - 1
            If attributes(i).GetType().Equals(GetType(MyAttribute)) Then
               Console.WriteLine(CType(attributes(i), MyAttribute).Name)
            End If
         Next i
      End If
   End Sub 'Main 
End Class 'LogicalMethodInfo_GetCustomAttribute

[C#] 
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);
      }
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.Web.Services.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Web::Services::Protocols;

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

   __property String* get_Name() {
      return myName;
   }
};

public __gc class MyService {
public:
   [MyAttribute(S"This is the first sample attribute")]
   [MyAttribute(S"This is the second sample attribute")]
   [returnvalue:MyAttribute(S"This is the return sample attribute")]

   int Add(int xValue, int yValue) {
      return (xValue + yValue);
   }
};

int main() {
   Type* myType = __typeof(MyService);
   MethodInfo* myMethodInfo = myType->GetMethod(S"Add");
   // Create a synchronous 'LogicalMethodInfo' instance.
   MethodInfo* temparray [] = {myMethodInfo};
   LogicalMethodInfo* myLogicalMethodInfo =
      (LogicalMethodInfo::Create(temparray,
      LogicalMethodTypes::Sync))[0];
   // Display the method for which the attributes are being displayed.
   Console::WriteLine(S"\nDisplaying the attributes for the method : {0}\n",
      myLogicalMethodInfo->MethodInfo);

   // Displaying a custom attribute of type 'MyAttribute'
   Console::WriteLine(S"\nDisplaying attribute of type 'MyAttribute'\n");
   Object* attribute = 
      myLogicalMethodInfo->GetCustomAttribute(__typeof(MyAttribute));
   Console::WriteLine((dynamic_cast<MyAttribute*>(attribute))->Name);

   // Display all custom attribute of type 'MyAttribute'.
   Console::WriteLine(S"\nDisplaying all attributes of type 'MyAttribute'\n");
   Object* attributes[] = 
      myLogicalMethodInfo->GetCustomAttributes(__typeof(MyAttribute));
   for (int i = 0; i < attributes->Length; i++)
      Console::WriteLine((dynamic_cast<MyAttribute*>(attributes->Item[i]))->Name);

   // Display all return attributes of type 'MyAttribute'.
   Console::WriteLine(S"\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->Item[i]->GetType()->Equals(__typeof(MyAttribute)))
            Console::WriteLine((dynamic_cast<MyAttribute*>(attributes->Item[i]))->Name);
   }

   // Display all the custom attributes of type 'MyAttribute'.
   Console::WriteLine(S"\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->Item[i]->GetType()->Equals(__typeof(MyAttribute)))
            Console::WriteLine((dynamic_cast<MyAttribute*>(attributes->Item[i]))->Name);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

LogicalMethodInfo クラス | LogicalMethodInfo メンバ | System.Web.Services.Protocols 名前空間