To call a protected static method on an instance of a class in C#, you can use reflection.
GetType()
method on an instance of the class.main.cs62 chars3 lines
GetMethod()
method to get the method info of the protected static method you want to call.main.cs132 chars3 lines
The first parameter of GetMethod()
is the name of the protected static method, and the second parameter is a BindingFlags
enumeration value that specifies that the method is not public and is static.
Invoke()
method.main.cs29 chars2 lines
Since the method is static, you can pass in null
for the first parameter of Invoke()
. The second parameter is an array of parameters to pass to the method, but since the protected static method has no parameters, we can pass in null
again.
Note: This approach is not recommended as it may violate the intended visibility of the method, and should be used judiciously.
gistlibby LogSnag