how to prevent getmethods from retrieve a specific method - c#

I have a static class with private functions, I want to get all functions except one. I tried using Ignorecase but i get an overload exception... I do it exactly like many examples online but I get an error and I dont know why...am I missing something?
//Example
static MethodInfo[] allFuncs ;
static Type myType = typeof(myClass);
allFuncs = myType.GetMethods("innerFunction",
BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Static);

If you want all methods except one with a particular name, you can use Enumerable.Where to filter:
allFuncs = typeof(MyClass).GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
.Where(method => !method.Name.Equals(
"innerFunction", StringComparison.OrdinalIgnoreCase));

Related

Programmatically get a list of method names in a form

I would like to be able to get a list of names of all the methods in a form (similar to the list obtained by pressing Alt+M).
I found the way to get only the names of the methods created by me.
Type methodInfoType = (typeof(Form_CreateNodes));
// Get the public methods.
MethodInfo[] arrayOfPublicMethodsNames = methodInfoType.GetMethods(
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
// Get the nonpublic methods.
MethodInfo[] arrayOfNonpublicMethodsNames = methodInfoType.GetMethods(
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

Proper use of Reflection and binding flags

I want to modify the below code to be able to use a private method
//use reflection to Load the data
var method =
typeof(MemberDataFactory)
.GetMethod("LoadData")
.MakeGenericMethod(new [] { data.GetType() })
.Invoke(this, null);
Ive tried the following with no luck:
//use reflection to Load the data
var method =
typeof(MemberDataFactory)
.GetMethod("LoadData")
.MakeGenericMethod(new [] { data.GetType() })
.Invoke(this, BindingFlags.Instance | BindingFlags.NonPublic, null , null, null);
Also what is "var" in terms of this code? Id prefer to specify its type instead of using var.
Thanks!
You want to use this overload of Type.GetMethod(), which is where you pass your binding flags. The default .GetMethod(string) only looks for public methods, so it returns null, hence your null reference exception.
Your code should be something more like:
var method =
typeof(MemberDataFactory)
.GetMethod("LoadData", BindingFlags.Instance | BindingFlags.NonPublic) // binding flags go here
...

Get Property by String Force Uppercase?

I am using the C# GetPropertymethod using reflection.
obj.GetType().GetProperty("columnName")
However I cannot guarantee the exact casing of the column name as it is being based in from an external source. It maybe ColumnName or columnname
I was thinking if I could just force the string column name to uppercase, but how would I then deal with the Property on the object itself? The getProperty method looks like it needs to be the EXACT casing?
You can use
var prop = GetProperty("columnname",
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.IgnoreCase);
Note that you'll still need the Instance and Public bit (assuming that this is a public instance property) as otherwise it won't find anything.
You can ignore the case when looking up the property.
GetProperty(fieldname, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
You can always combine reflection with some Linq magic, like this:
var property = typeof (MyType).GetProperties()
.Where(p => p.Name.Equals("MyProperty", StringComparison.InvariantCultureIgnoreCase));
Try:
var yourprop = from x in obj.GetType().GetProperties()
where x.Name.ToUpper() == "a column name".ToUpper()
select x;

How can we get reference to current HttpRuntime?

Can anybody tell me what is this code doing:
PropertyInfo p = typeof(HttpRuntime).GetProperty("FileChangesMonitor",
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
object o = p.GetValue(null, null);
FieldInfo f = o.GetType().GetField("_dirMonSubdirs",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
MethodInfo m = monitor.GetType().GetMethod("StopMonitoring",
BindingFlags.Instance | BindingFlags.NonPublic);
m.Invoke(monitor, new object[] { });
How did the person get these reflection field names?
Effectively, the code is doing the equivalent of:
dynamic o = HttpRuntime.FileChangesMonitor;
dynamic monitor = o._dirMonSubdirs;
monitor.StopMonitoring();
The BindingFlags.NonPublic allow, through the use of reflection, accessing nonpublic fields. Without reflection, the above code would generate a compiler error.
The reflection field names can be obtained through a debugger, or types can be enumerated through reflection. For instance, to get all public and nonpublic static fields of a type X, you could use:
MemberInfo[] mi = typeof(X).GetType().FindMembers(MemberTypes.Property,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
(a, b) => true, // don't filter
null);
Note that using reflection to access nonpublic members is generally considered poor practice, since doing so relies on implementation mechanics that are not guaranteed and that are allowed to change from version to version and between implementations.

Why can't I call this method via string?

Question from a Reflection newbie. I have a method in a Windows Form:
private void handleOrderCode()
{
//...do stuff
}
Which I am trying to call in the following manner:
Type t = this.GetType();
MethodInfo mi = t.GetMethod("handleOrderCode");
if (mi != null) mi.Invoke(this, null);
I have confirmed that "this" is not null. The space where the string "handleOrderCode" has been hardcoded is to be replaced with a string variable when this works. However, at present "mi" is always null when it evaluates in the if statement in the final line.
So what am I doing wrong?
You need to specify binding flags:
using System.Reflection;
t.GetMethod("handleOrderCode", BindingFlags.Instance | BindingFlags.NonPublic)
Because overload without any flag means:
BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance
i.e. will not return any non-public (private, protected, etc) members.
The parameterless overload of Type.GetMethod only looks for public methods:
Searches for the public method with the specified name.
You need to specify an appropriate BindingFlags value to another overload instead:
MethodInfo method = t.GetMethod("handleOrderCode",
BindingFlags.Instance | BindingFlags.NonPublic);
Note that you need to specify "instance" or "static" here (or both), not just "non-public". If you want to look for public methods as well, you have to include that too.
Another alternative is just to make your method public :)
(Additionally, I'd suggest renaming it to HandleOrderCode to be more conventional, idiomatic C#.)
try:
Type t = this.GetType();
MethodInfo mi = t.GetMethod("handleOrderCode",
BindingFlags.NonPublic | BindingFlags.Instance);
if (mi != null) mi.Invoke(this, null);

Categories