LogicalMethodInfo.GetCustomAttributes Method

Returns the custom attributes applied to the specified type.

Namespace: System.Web.Services.Protocols
Assembly: System.Web.Services (in system.web.services.dll)

public:
array<Object^>^ GetCustomAttributes (
	Type^ type
)
public Object[] GetCustomAttributes (
	Type type
)
public function GetCustomAttributes (
	type : Type
) : Object[]
Not applicable.

Parameters

type

The Type to which the custom attributes are applied.

Return Value

An array of Object containing the custom attributes applied to type.

Exception typeCondition

TypeLoadException

The custom attribute type can not be loaded.

#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 ref class MyAttribute: public Attribute
{
private:
   String^ myName;

public:
   MyAttribute( String^ name )
   {
      myName = name;
   }

   property String^ Name 
   {
      String^ get()
      {
         return myName;
      }
   }
};

public ref class MyService
{
public:

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

int main()
{
   Type^ myType = MyService::typeid;
   MethodInfo^ myMethodInfo = myType->GetMethod( "Add" );

   // Create a synchronous 'LogicalMethodInfo' instance.
   array<MethodInfo^>^temparray = {myMethodInfo};
   LogicalMethodInfo^ myLogicalMethodInfo = (LogicalMethodInfo::Create( temparray, 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( MyAttribute::typeid );
   Console::WriteLine( (dynamic_cast<MyAttribute^>(attribute))->Name );

   // Display all custom attribute of type 'MyAttribute'.
   Console::WriteLine( "\nDisplaying all attributes of type 'MyAttribute'\n" );
   array<Object^>^attributes = myLogicalMethodInfo->GetCustomAttributes( MyAttribute::typeid );
   for ( int i = 0; i < attributes->Length; i++ )
      Console::WriteLine( (dynamic_cast<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( MyAttribute::typeid, true ) )
   {
      attributes = myCustomAttributeProvider->GetCustomAttributes( true );
      for ( int i = 0; i < attributes->Length; i++ )
         if ( attributes[ i ]->GetType()->Equals( MyAttribute::typeid ) )
                  Console::WriteLine( (dynamic_cast<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( MyAttribute::typeid, true ) )
   {
      attributes = myCustomAttributeProvider->GetCustomAttributes( true );
      for ( int i = 0; i < attributes->Length; i++ )
         if ( attributes[ i ]->GetType()->Equals( MyAttribute::typeid ) )
                  Console::WriteLine( (dynamic_cast<MyAttribute^>(attributes[ i ]))->Name );
   }
}

import System.*;
import System.Reflection.*;
import System.Web.Services.Protocols.*;

// Define a custom attribute with one named parameter.
/** @attribute AttributeUsage(AttributeTargets.Method 
    | AttributeTargets.ReturnValue, AllowMultiple = true)
 */
public class MyAttribute extends Attribute
{
    private String myName;

    public MyAttribute(String name)
    {
        myName = name;
    } //MyAttribute

    /** @property 
     */
    public String get_Name()
    {
        return myName;
    } //get_Name
} //MyAttribute

public class MyService
{
    /** @attribute MyAttribute("This is the first sample attribute")
     */
    /** @attribute MyAttribute("This is the second sample attribute")
     */
    /** @attribute.return MyAttribute("This is the return sample attribute")
     */
    public int Add(int xValue, int yValue)
    {
        return xValue + yValue;
    } //Add
} //MyService

public class LogicalMethodInfo_GetCustomAttribute
{
    public static void main(String[] args)
    {
        Type myType = MyService.class.ToType();
        MethodInfo myMethodInfo = myType.GetMethod("Add");

        // Create a synchronous 'LogicalMethodInfo' instance.
        LogicalMethodInfo myLogicalMethodInfo = 
            (LogicalMethodInfo)LogicalMethodInfo.Create(new MethodInfo[] 
            { myMethodInfo }, LogicalMethodTypes.Sync).get_Item(0);

        // Display the method for which the attributes are being displayed.
        Console.WriteLine("\nDisplaying the attributes for the method : {0}\n",
            myLogicalMethodInfo.get_MethodInfo());

        // Displaying a custom attribute of type 'MyAttribute'
        Console.WriteLine("\nDisplaying attribute of type 'MyAttribute'\n");
        Object attribute = myLogicalMethodInfo.GetCustomAttribute(
            MyAttribute.class.ToType());
        Console.WriteLine(((MyAttribute)attribute).get_Name());

        // Display all custom attribute of type 'MyAttribute'.
        Console.WriteLine("\nDisplaying all attributes of type "
            + "'MyAttribute'\n");
        Object attributes[] = 
            myLogicalMethodInfo.GetCustomAttributes(MyAttribute.class.ToType());
        for (int i = 0; i < attributes.length; i++) {
            Console.WriteLine(((MyAttribute)attributes.get_Item(i)).get_Name());
        }
        // Display all return attributes of type 'MyAttribute'.
        Console.WriteLine("\nDisplaying all return attributes "
            + "of type 'MyAttribute'\n");
        ICustomAttributeProvider myCustomAttributeProvider = 
            myLogicalMethodInfo.get_ReturnTypeCustomAttributeProvider();
        if (myCustomAttributeProvider.IsDefined(MyAttribute.class.ToType(), 
            true)) {
            attributes = myCustomAttributeProvider.GetCustomAttributes(true);
            for (int i = 0; i < attributes.length; i++) {
                if (attributes.get_Item(i).GetType().
                    Equals(MyAttribute.class.ToType())) {
                    Console.WriteLine(((MyAttribute)(attributes.get_Item(i))).
                        get_Name());
                }
            }
        }

        // Display all the custom attributes of type 'MyAttribute'.
        Console.WriteLine("\nDisplaying all attributes of type 'MyAttribute'\n");
        myCustomAttributeProvider = 
            myLogicalMethodInfo.get_CustomAttributeProvider();
        if (myCustomAttributeProvider.IsDefined(MyAttribute.class.ToType(), 
            true)) {
            attributes = myCustomAttributeProvider.GetCustomAttributes(true);
            for (int i = 0; i < attributes.length; i++) {
                if (attributes.get_Item(i).GetType().
                    Equals(MyAttribute.class.ToType())) {
                    Console.WriteLine(((MyAttribute)attributes.get_Item(i)).
                        get_Name());
                }
            }
        }
    } //main
} //LogicalMethodInfo_GetCustomAttribute

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: