using namespace System;
using namespace System::Collections;
using namespace System::Reflection;
using namespace System::IO;
//namespace BindingFlagsSnippet {
public ref class TestClass
{
public:
String^ Name;
private:
array<Object^>^values;
public:
property Object^ Item [int]
{
Object^ get( int index )
{
return values[ index ];
}
void set( int index, Object^ value )
{
values[ index ] = value;
}
}
property Object^ Value
{
Object^ get()
{
return "the value";
}
}
int methodCalled;
TestClass()
{
Name = "initialName";
array<Object^>^o = {(int^)0,1,2,3,4,5,6,7,8,9};
values = o;
methodCalled = 0;
}
static void SayHello()
{
Console::WriteLine( "Hello" );
}
void AddUp()
{
methodCalled++;
Console::WriteLine( "AddUp Called {0} times", methodCalled );
}
static double ComputeSum( double d1, double d2 )
{
return d1 + d2;
}
static void PrintName( String^ firstName, String^ lastName )
{
Console::WriteLine( "{0},{1}", lastName, firstName );
}
void PrintTime()
{
Console::WriteLine( DateTime::Now );
}
void Swap( interior_ptr<int> a, interior_ptr<int> b )
{
int x = *a;
*a = *b;
*b = x;
}
};
[DefaultMemberAttribute("PrintTime")]
public ref class TestClass2
{
public:
void PrintTime()
{
Console::WriteLine( DateTime::Now );
}
};
class Invoke
{
public:
static void Go()
{
// BindingFlags::InvokeMethod
// Call a static method.
Type^ t = TestClass::typeid;
Console::WriteLine();
Console::WriteLine( "Invoking a static method." );
Console::WriteLine( "-------------------------" );
array<Object^>^obj1;
t->InvokeMember( "SayHello", BindingFlags::InvokeMethod | BindingFlags::Public | BindingFlags::Static, nullptr, nullptr, obj1 );
// BindingFlags::InvokeMethod
// Call an instance method.
TestClass^ c = gcnew TestClass;
Console::WriteLine();
Console::WriteLine( "Invoking an instance method." );
Console::WriteLine( "----------------------------" );
c->GetType()->InvokeMember( "AddUp", BindingFlags::InvokeMethod, nullptr, c, obj1 );
c->GetType()->InvokeMember( "AddUp", BindingFlags::InvokeMethod, nullptr, c, obj1 );
// BindingFlags::InvokeMethod
// Call a method with parameters.
array<Object^>^args = {100.09,184.45};
Object^ result;
Console::WriteLine();
Console::WriteLine( "Invoking a method with parameters." );
Console::WriteLine( "---------------------------------" );
result = t->InvokeMember( "ComputeSum", BindingFlags::InvokeMethod, nullptr, nullptr, args );
Console::WriteLine( " {0} + {1} = {2}", args[ 0 ], args[ 1 ], result );
// BindingFlags::GetField, SetField
Console::WriteLine();
Console::WriteLine( "Invoking a field (getting and setting.)" );
Console::WriteLine( "--------------------------------------" );
// Get a field value.
result = t->InvokeMember( "Name", BindingFlags::GetField, nullptr, c, obj1 );
Console::WriteLine( "Name == {0}", result );
// Set a field.
array<Object^>^obj2 = {"NewName"};
t->InvokeMember( "Name", BindingFlags::SetField, nullptr, c, obj2 );
result = t->InvokeMember( "Name", BindingFlags::GetField, nullptr, c, obj1 );
Console::WriteLine( "Name == {0}", result );
Console::WriteLine();
Console::WriteLine( "Invoking an indexed property (getting and setting.)" );
Console::WriteLine( "--------------------------------------------------" );
// BindingFlags::GetProperty
// Get an indexed property value.
int index = 3;
array<Object^>^obj3 = {index};
result = t->InvokeMember( "Item", BindingFlags::GetProperty, nullptr, c, obj3 );
Console::WriteLine( "Item->Item[ {0}] == {1}", index, result );
// BindingFlags::SetProperty
// Set an indexed property value.
index = 3;
array<Object^>^obj4 = {index,"NewValue"};
t->InvokeMember( "Item", BindingFlags::SetProperty, nullptr, c, obj4 );
result = t->InvokeMember( "Item", BindingFlags::GetProperty, nullptr, c, obj3 );
Console::WriteLine( "Item->Item[ {0}] == {1}", index, result );
Console::WriteLine();
Console::WriteLine( "Getting a field or property." );
Console::WriteLine( "----------------------------" );
// BindingFlags::GetField
// Get a field or property.
result = t->InvokeMember( "Name", static_cast<BindingFlags>(BindingFlags::GetField | BindingFlags::GetProperty), nullptr, c, obj1 );
Console::WriteLine( "Name == {0}", result );
// BindingFlags::GetProperty
result = t->InvokeMember( "Value", static_cast<BindingFlags>(BindingFlags::GetField | BindingFlags::GetProperty), nullptr, c, obj1 );
Console::WriteLine( "Value == {0}", result );
Console::WriteLine();
Console::WriteLine( "Invoking a method with named parameters." );
Console::WriteLine( "---------------------------------------" );
// BindingFlags::InvokeMethod
// Call a method using named parameters.
array<Object^>^argValues = {"Mouse","Micky"};
array<String^>^argNames = {"lastName","firstName"};
t->InvokeMember( "PrintName", BindingFlags::InvokeMethod, nullptr, nullptr, argValues, nullptr, nullptr, argNames );
Console::WriteLine();
Console::WriteLine( "Invoking a default member of a type." );
Console::WriteLine( "------------------------------------" );
// BindingFlags::Default
// Call the default member of a type.
Type^ t3 = TestClass2::typeid;
t3->InvokeMember( "", static_cast<BindingFlags>(BindingFlags::InvokeMethod | BindingFlags::Default), nullptr, gcnew TestClass2, obj1 );
// BindingFlags::Static, NonPublic, and Public
// Invoking a member by reference.
Console::WriteLine();
Console::WriteLine( "Invoking a method by reference." );
Console::WriteLine( "-------------------------------" );
MethodInfo^ m = t->GetMethod( "Swap" );
args = gcnew array<Object^>(2);
args[ 0 ] = 1;
args[ 1 ] = 2;
m->Invoke( gcnew TestClass, args );
Console::WriteLine( "{0}, {1}", args[ 0 ], args[ 1 ] );
// The String* is case-sensitive.
Type^ type = Type::GetType( "System.String" );
// Check to see if the value is valid. If the Object* is 0, the type does not exist.
if ( type == nullptr )
{
Console::WriteLine( "Please ensure that you specify only valid types in the type field." );
Console::WriteLine( "The type name is case-sensitive." );
return;
}
// Declare and populate the arrays to hold the information.
// You must declare either NonPublic or Public with Static or the search will not work.
array<FieldInfo^>^fi = type->GetFields( static_cast<BindingFlags>(BindingFlags::Static | BindingFlags::NonPublic | BindingFlags::Public) );
// BindingFlags::NonPublic
array<MethodInfo^>^miNonPublic = type->GetMethods( static_cast<BindingFlags>(BindingFlags::Static | BindingFlags::NonPublic) );
// BindingFlags::Public
array<MethodInfo^>^miPublic = type->GetMethods( static_cast<BindingFlags>(BindingFlags::Static | BindingFlags::Public) );
// Iterate through all the nonpublic methods.
IEnumerator^ myEnum1 = miNonPublic->GetEnumerator();
while ( myEnum1->MoveNext() )
{
MethodInfo^ method = safe_cast<MethodInfo^>(myEnum1->Current);
Console::WriteLine( method );
}
IEnumerator^ myEnum2 = miPublic->GetEnumerator();
while ( myEnum2->MoveNext() )
{
MethodInfo^ method = safe_cast<MethodInfo^>(myEnum2->Current);
Console::WriteLine( method );
}
IEnumerator^ myEnum3 = fi->GetEnumerator();
while ( myEnum3->MoveNext() )
{
FieldInfo^ f = safe_cast<FieldInfo^>(myEnum3->Current);
Console::WriteLine( f );
}
// BindingFlags::Instance
// Call an instance method.
TestClass^ tc = gcnew TestClass;
Console::WriteLine();
Console::WriteLine( "Invoking an Instance method." );
Console::WriteLine( "----------------------------" );
tc->GetType()->InvokeMember( "AddUp", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance | BindingFlags::CreateInstance), nullptr, tc, obj1 );
// BindingFlags::CreateInstance
// Calling and creating an instance method.
Console::WriteLine();
Console::WriteLine( "Invoking and creating an instance method." );
Console::WriteLine( "-----------------------------------------" );
tc->GetType()->InvokeMember( "AddUp", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance | BindingFlags::CreateInstance), nullptr, tc, obj1 );
// BindingFlags::DeclaredOnly
TestClass^ tc2 = gcnew TestClass;
Console::WriteLine();
Console::WriteLine( "DeclaredOnly members" );
Console::WriteLine( "---------------------------------" );
array<System::Reflection::MemberInfo^>^memInfo = tc2->GetType()->GetMembers( BindingFlags::DeclaredOnly );
for ( int i = 0; i < memInfo->Length; i++ )
{
Console::WriteLine( memInfo[ i ]->Name );
}
// BindingFlags::SuppressChangeType
TestClass^ obj = gcnew TestClass;
Console::WriteLine();
Console::WriteLine( "Invoking static method - PrintName" );
Console::WriteLine( "---------------------------------" );
System::Reflection::MethodInfo^ methInfo = obj->GetType()->GetMethod( "PrintName" );
array<Object^>^args1 = {"Brad","Smith"};
methInfo->Invoke( obj, static_cast<BindingFlags>(BindingFlags::SuppressChangeType | BindingFlags::InvokeMethod), nullptr, args1, nullptr );
// BindingFlags::IgnoreCase
Console::WriteLine();
Console::WriteLine( "Using IgnoreCase and invoking the PrintName method." );
Console::WriteLine( "---------------------------------------------------" );
methInfo = obj->GetType()->GetMethod( "PrintName" );
array<Object^>^args2 = {"brad","smith"};
methInfo->Invoke( obj, static_cast<BindingFlags>(BindingFlags::IgnoreCase | BindingFlags::InvokeMethod), nullptr, args2, nullptr );
// BindingFlags::IgnoreReturn
Console::WriteLine();
Console::WriteLine( "Using IgnoreReturn and invoking the PrintName method." );
Console::WriteLine( "-----------------------------------------------------" );
methInfo = obj->GetType()->GetMethod( "PrintName" );
array<Object^>^args3 = {"Brad","Smith"};
methInfo->Invoke( obj, static_cast<BindingFlags>(BindingFlags::IgnoreReturn | BindingFlags::InvokeMethod), nullptr, args3, nullptr );
// BindingFlags::OptionalParamBinding
Console::WriteLine();
Console::WriteLine( "Using OptionalParamBinding and invoking the PrintName method." );
Console::WriteLine( "-------------------------------------------------------------" );
methInfo = obj->GetType()->GetMethod( "PrintName" );
array<Object^>^args4 = {"Brad","Smith"};
methInfo->Invoke( obj, static_cast<BindingFlags>(BindingFlags::OptionalParamBinding | BindingFlags::InvokeMethod), nullptr, args4, nullptr );
// BindingFlags::ExactBinding
Console::WriteLine();
Console::WriteLine( "Using ExactBinding and invoking the PrintName method." );
Console::WriteLine( "-----------------------------------------------------" );
methInfo = obj->GetType()->GetMethod( "PrintName" );
array<Object^>^args5 = {"Brad","Smith"};
methInfo->Invoke( obj, static_cast<BindingFlags>(BindingFlags::ExactBinding | BindingFlags::InvokeMethod), nullptr, args5, nullptr );
// BindingFlags::FlattenHierarchy
Console::WriteLine();
Console::WriteLine( "Using FlattenHierarchy and invoking the PrintName method." );
Console::WriteLine( "---------------------------------------------------------" );
methInfo = obj->GetType()->GetMethod( "PrintName" );
array<Object^>^args6 = {"Brad","Smith"};
methInfo->Invoke( obj, static_cast<BindingFlags>(BindingFlags::FlattenHierarchy | BindingFlags::InvokeMethod), nullptr, args6, nullptr );
}
};
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
Invoke::Go();
}
//}