Here’s some sample code showing how you can do this; the example calls
System::String::InternalLength which is private.
Console::WriteLine(safe_cast<int>( __typeof(String)->InvokeMember("InternalLength", BindingFlags::InvokeMethod | BindingFlags::NonPublic | BindingFlags::Instance, nullptr,gcnew String("hello world"), gcnew array<Object^>(0))));
Of course, in the above case what we did is not particularly useful since we could simply have used the
Length property which internally calls
InternalLength, but it’s just to show how it’s done 🙂