I am working on an extension to an existing program, but the main program's extension API doesn't expose a particular method that I am trying to access. I have the access to expose this to the API and submit the revision to the main code base, but I am interested in finding a resolution that works with the existing code as well.
Presently there is a class defined such as this in the main program (the class itself is not presently exposed to the API):
namespace Program
{
class TextControl
{
// ...
public void SetText(string newText)
{
SetText(newText, true);
}
public void SetText(string newText, bool clearModified)
{
// ...
}
}
}
I need to be able to leave the modified flag in-tact, so using the single-parameter overload is insufficient. Since the class and its methods are not exposed to the API, I am trying to use reflection to access the function:
try
{
Type t = Assembly.GetEntryAssembly().GetType("Program.TextControl"); // this is working fine
MethodInfo method = t.GetMethod("SetText", new Type[] { typeof(string), typeof(bool) });
if (method == null) System.Windows.Forms.MessageBox.Show("Method not found.");
else System.Windows.Forms.MessageBox.Show("Method " + method.Name + " found.");
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Error:" + Environment.NewLine + Environment.NewLine + e.Message);
}
This code is always showing "Method not found.". So no exceptions are being thrown, but the overloaded method is not being found. So I tried this:
try
{
Type t = Assembly.GetEntryAssembly().GetType("Program.TextControl"); // this is working fine
MethodInfo method = t.GetMethod("SetText", BindingFlags.Public | BindingFlags.Instance, Type.DefaultBinder, new Type[] { typeof(string), typeof(bool) }, new ParameterModifier[0]);
if (method == null) System.Windows.Forms.MessageBox.Show("Method not found.");
else System.Windows.Forms.MessageBox.Show("Method " + method.Name + " found.");
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Error:" + Environment.NewLine + Environment.NewLine + e.Message);
}
My results were identical. They were also identical if I pass null as the Binder or ParameterModifier[] parameters.
So from there I was somewhat stumped, but I decided to iterate through every method and see what was being found:
try
{
Type t = Assembly.GetEntryAssembly().GetType("Program.TextControl"); // this is working fine
foreach (MethodInfo method in t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
if (method.Name == "SetText")
{
System.Windows.Forms.MessageBox.Show("Method SetText found with " + method.GetParameters().Length.ToString() + " parameters.");
}
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Error:" + Environment.NewLine + Environment.NewLine + e.Message);
}
Still no exceptions being thrown, this only shows one method as being found: "Method SetText found with 1 parameters.".
So I displayed every found method, and confirmed again that only one instance of SetText was being found.
From there, I started searching on Google, and found this on Stack Overflow, where someone suggests to use the InvokeMember function instead.
try
{
Type t = Assembly.GetEntryAssembly().GetType("Program.TextControl"); // this is working fine
// myInstance = exposed base interface instance, this is working fine
t.InvokeMember("SetText", BindingFlags.InvokeMethod, Type.DefaultBinder, myInstance, new object[] { newText, false });
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("Error:" + Environment.NewLine + Environment.NewLine + e.Message);
}
With this, an exception is being thrown:
Quote
---------------------------
---------------------------
Error:
Method 'Program.TextControl.SetText' not found.
---------------------------
OK
---------------------------
---------------------------
Error:
Method 'Program.TextControl.SetText' not found.
---------------------------
OK
---------------------------
I have successfully reflected into other members of the same class, but I am having difficulty in finding this overload of this particular method. Other methods within the same class I am able to successfully find overloads of. If I have overlooked anything, I would appreciate a second set of eyes, though even as I've been typing this, I don't see where I have. Beyond that, if there's any reason why a particular overload of a particular method would not be able to be found by reflection, while other methods (including the other overload of the same method in the class) are able to be found, I would also appreciate that information.
Thanks in advance.
This post has been edited by monkey_05_06: 09 June 2012 - 01:39 PM

New Topic/Question
Reply




MultiQuote




|