Mike_R Posted October 4, 2006 Posted October 4, 2006 Hi guys, I'm trying to get a method an invoke it, ignoring the case of the name passed in. The following works 100% fine: Dim procName As String procName = "MyMethod" '<-- Or whatever is to be called. Dim paramTypes As Type() ' I then set the paramTypes for the arguments properly...Ok, and then we make the following call: Dim methInfo As Reflection.MethodInfo = _ objectReference.GetType.GetMethod(procName, paramTypes) The above works fine. But the call is case-sensitive with respect to the procName, which is "MyMethod" in this case. Often it's hard to know up front if a method is named, say, "SubString" or "Substring" and here it would matter. A lot. As a VB programmer this case sensitivity is more than a little annoying. So I then attempted the following, which makes use of binding flags, in particular the 'System.Reflection.BindingFlags.IgnoreCase' binding flag: Dim methInfo As Reflection.MethodInfo = _ objectReference.GetType.GetMethod( _ procName, _ System.Reflection.BindingFlags.IgnoreCase Or _ System.Reflection.BindingFlags.Public Or _ System.Reflection.BindingFlags.Instance, _ Type.DefaultBinder, _ paramTypes, _ Nothing) The above works perfectly too... However, it does *NOT* ignore case! This would seem to be the point of 'BindingFlags.IgnoreCase', right?? Now this might not be a brilliant thing for me to be doing, for I assume that a C# assembly could have two methods with the same name only differing by case. (Although, they should not both be public in this situation, but theoretically could be.) I really wonder how VB would deal with calling into a C# assembly in this situation with early-bound calls, never mind late bound. But for the moment, I'm willing to assume that the call to the Method is safe as a case-insensitive call. So any ideas on how I can get 'System.Reflection.BindingFlags.IgnoreCase' to work? Thanks all in advance... Mike Quote Posting Guidelines Avatar by Lebb
Leaders snarfblam Posted October 5, 2006 Leaders Posted October 5, 2006 Sorry its C#, but this appearently equivalent code worked just fine for me: public partial class Form1:Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MethodInfo Foofo = this.GetType().GetMethod( "FOO", BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance, Type.DefaultBinder, new Type[] { }, null); Foofo.Invoke(this, null); } public void foo() { MessageBox.Show("Bar."); } } Note that I use the same binding flags and the method is declared as "foo" but searched for as "FOO". Quote [sIGPIC]e[/sIGPIC]
Mike_R Posted October 8, 2006 Author Posted October 8, 2006 Thanks, Marble, for trying some test code... So it looks like we did the same thing. I'll have to try that again, I might have been tired and did something silly. I'm swamped right now, but when I can, I'll try it in both C# and VB.NET and we'll see. If I get anything interesting, I'll post back... Thanks ME Quote Posting Guidelines Avatar by Lebb
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.