This documentation is archived and is not being maintained.
LogicalMethodInfo::GetCustomAttribute Method
Visual Studio 2008
Returns the first custom attribute applied to the type, if any custom attributes are applied to the type.
Assembly: System.Web.Services (in System.Web.Services.dll)
Parameters
- type
- Type: System::Type
The Type to which the custom attributes are applied.
Return Value
Type: System::ObjectAn Object containing the first custom attribute applied to the type parameter.
| Exception | Condition |
|---|---|
| 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 ); } }
#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);
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: