I have a problem getting a private method using reflection.
Even with BindingFlags.NonPublic and BindingFlags.Instance it doesnt work.
The HandleClientDrivenStatePropertyChanged is defined on the same class as the CreateRadioPropertyInstances method.
class Program
{
static void Main(string[] args)
{
RadioPropertiesState state = new RadioPropertiesState();
}
}
internal class RadioPropertiesState : BaseRadioPropertiesState
{
}
internal class BaseRadioPropertiesState
{
public BaseRadioPropertiesState()
{
CreateRadioPropertyInstances();
}
private void CreateRadioPropertyInstances()
{
// get the method that is subscribed to the changed event
MethodInfo changedEventHandlerInfo = GetType().GetMethod(
"HandleClientDrivenStatePropertyChanged",
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.IgnoreCase);
}
private void HandleClientDrivenStatePropertyChanged
(object sender, EventArgs e)
{
}
}
GetMethod returns null.
What can be the problem?
[edited code]
The problem is exactly as I suggested in my comment – you’re trying to find the method based on the execution time type of the object, which is
RadioPropertiesState… but it’s not declared in that type or visible to it.Change your
GetMethodcall to:and it works fine.