using System;
using System.Reflection;
namespace Reflection
{
class Test
{
protected void methodname()
{
Console.WriteLine(("in the world of the reflection"));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
// BindingFlags eFlags = BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public|BindingFlags.NonPublic;
BindingFlags eFlags = BindingFlags.Instance|BindingFlags.NonPublic;
Test aTest = new Test();
MethodInfo mInfoMethod = typeof(Reflection.Test).GetMethod("methodname", eFlags);
mInfoMethod.Invoke(aTest, null);
}
}
}
As per the msdn BindingFlags.Nonpublic is used to access the non private members. If I use only this enum the Getmethod returns null value. But if use the enum - Instance and nonpublic the required method is called. What is the difference between these two. When I have to use instance and public/nonpublic combination.
Per the documentation of GetMethod()
:
You must specify either
BindingFlags.Instance
orBindingFlags.Static
in order to get a return.
Instance
/Static
and Public
/NonPublic
specify two different things and you have to specify both in order to get the result.
If you don't specify the enum, the default is used. If you do, you have to specify both:
(See the note in the remarks section on http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With