PropertyInfo::GetAccessors Method (Boolean)

 

Returns an array whose elements reflect the public and, if specified, non-public get and set accessors of the property reflected by the current instance.

Namespace:   System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public:
virtual array<MethodInfo^>^ GetAccessors(
	bool nonPublic
) abstract

Parameters

nonPublic
Type: System::Boolean

Indicates whether non-public methods should be returned in the returned array. true if non-public methods are to be included; otherwise, false.

Return Value

Type: array<System.Reflection::MethodInfo^>^

An array whose elements reflect the get and set accessors of the property reflected by the current instance. If nonPublic is true, this array contains public and non-public get and set accessors. If nonPublic is false, this array contains only public get and set accessors. If no accessors with the specified visibility are found, this method returns an array with zero (0) elements.

To call the GetAccessors method:

  1. Get a Type object that represents the class.

  2. From the Type object, get the PropertyInfo object.

  3. From the PropertyInfo object, call the GetAccessors method.

The following example retrieves the accessors of the ClassWithProperty.Caption property and displays information about them. It also calls the Invoke method of the setter to set the property value and of the getter to retrieve the property value.

using namespace System;
using namespace System::Reflection;

// Define a property.
public ref class Myproperty
{
private:
   String^ caption;

public:
   Myproperty()
      : caption( "A Default caption" )
   {}


   property String^ Caption 
   {
      String^ get()
      {
         return caption;
      }

      void set( String^ value )
      {
         if ( caption != value )
         {
            caption = value;
         }
      }

   }

};

int main()
{
   Console::WriteLine( "\nReflection.PropertyInfo" );

   // Get the type and PropertyInfo.
   Type^ MyType = Type::GetType( "Myproperty" );
   PropertyInfo^ Mypropertyinfo = MyType->GetProperty( "Caption" );

   // Get the public GetAccessors method.
   array<MethodInfo^>^Mymethodinfoarray = Mypropertyinfo->GetAccessors( true );
   Console::Write( "\nThere are {0} accessors (public).", Mymethodinfoarray->Length );
   return 0;
}

ReflectionPermission

when invoked late-bound through mechanisms such as Type::InvokeMember. Associated enumeration: ReflectionPermissionFlag::MemberAccess.

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: