Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posting Guidelines

 

Avatar by Lebb

  • Leaders
Posted

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".

[sIGPIC]e[/sIGPIC]
Posted

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

Posting Guidelines

 

Avatar by Lebb

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...