HI, all,
I just want to invoke an internal method named 'ResolveClientUrl', which
is defined in class System.Web.UI.Control, using an instance object of a
type that derives from Control.
Let's see the following code snippet (class MyControl is a sub class that
derives directly from System.Web.UI.Control):
//...
MyControl instance = new MyControl();
Type type = instance.GetType();
object[] args = new object[] {"images/test.gif"};
string path = (string)type.InvokeMember("ResolveClientUrl",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.GetProperty,
null, instance, args);
//...
Actually, I intended to use the type object of MyControl to invoke the inherited
method 'ResolveClientUrl', but an Exception occured, described as "Method
LaserWeb.Presentation.MyControl.ResolveClientUrl not found." It seems that
reflection didn't search in the inherited members up the class hierarchy,
since 'ResolveClientUrl' is a declared method in the base class Control.
This action, I guess, should only be taken when BindingFlags.DeclaredOnly
is specified for invoking a method, otherwise inherited members derived from
base class should be considered. However, in this sample code, it didn't
work:( I did not pass the BindingFlags.DeclaredOnly flag. Then why? How could
it happen? I was puzzled:( Help!
Regards,
Laser Lu 6 2814
Laser Lu <la******@hotmail.com> wrote: HI, all, I just want to invoke an internal method named 'ResolveClientUrl', which is defined in class System.Web.UI.Control, using an instance object of a type that derives from Control.
Let's see the following code snippet (class MyControl is a sub class that derives directly from System.Web.UI.Control): //... MyControl instance = new MyControl(); Type type = instance.GetType(); object[] args = new object[] {"images/test.gif"}; string path = (string)type.InvokeMember("ResolveClientUrl", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, instance, args); //...
Actually, I intended to use the type object of MyControl to invoke the inherited method 'ResolveClientUrl', but an Exception occured, described as "Method LaserWeb.Presentation.MyControl.ResolveClientUrl not found." It seems that reflection didn't search in the inherited members up the class hierarchy, since 'ResolveClientUrl' is a declared method in the base class Control.
This action, I guess, should only be taken when BindingFlags.DeclaredOnly is specified for invoking a method, otherwise inherited members derived from base class should be considered. However, in this sample code, it didn't work:( I did not pass the BindingFlags.DeclaredOnly flag. Then why? How could it happen? I was puzzled:( Help!
Even if you could find the member, you shouldn't be able to invoke it,
if it's internal to a separate assembly.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hello Jon Skeet, actually, the following code runs well:
//...
MyControl instance = new MyControl();
Type type = typeof(System.Web.UI.Control); // here use the Type object of
Control, not of MyControl.
object[] args = new object[] {"images/test.gif"};
string path = (string)type.InvokeMember("ResolveClientUrl",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.GetProperty,
null, instance, args);
//...
The only change, that I've made on the code, is that I used the Type object
of Control instead. And the Control class is the declaring type of the member
'ResolveClientUrl'.
Maybe I didn't understand what do you mean in your post. However, the above
code runs well without any exceptions, and the method member gets invoked
correctly, even it is internal to my assembly.
Still, My puzzle is how to invoke inherited members using derived types?
It seems that there's no difference in cases whether you have a BindingFlags.DeclareOnly
specified or not:( Why is it so strange? Laser Lu <la******@hotmail.com> wrote:
HI, all, I just want to invoke an internal method named 'ResolveClientUrl', which is defined in class System.Web.UI.Control, using an instance object of a type that derives from Control. Let's see the following code snippet (class MyControl is a sub class that derives directly from System.Web.UI.Control): //... MyControl instance = new MyControl(); Type type = instance.GetType(); object[] args = new object[] {"images/test.gif"}; string path = (string)type.InvokeMember("ResolveClientUrl", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, instance, args); //... Actually, I intended to use the type object of MyControl to invoke the inherited method 'ResolveClientUrl', but an Exception occured, described as "Method LaserWeb.Presentation.MyControl.ResolveClientUrl not found." It seems that reflection didn't search in the inherited members up the class hierarchy, since 'ResolveClientUrl' is a declared method in the base class Control.
This action, I guess, should only be taken when BindingFlags.DeclaredOnly is specified for invoking a method, otherwise inherited members derived from base class should be considered. However, in this sample code, it didn't work:( I did not pass the BindingFlags.DeclaredOnly flag. Then why? How could it happen? I was puzzled:( Help! Even if you could find the member, you shouldn't be able to invoke it, if it's internal to a separate assembly.
Laser Lu <la******@hotmail.com> wrote: Hello Jon Skeet, actually, the following code runs well: //... MyControl instance = new MyControl(); Type type = typeof(System.Web.UI.Control); // here use the Type object of Control, not of MyControl. object[] args = new object[] {"images/test.gif"}; string path = (string)type.InvokeMember("ResolveClientUrl", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, instance, args); //...
The only change, that I've made on the code, is that I used the Type object of Control instead. And the Control class is the declaring type of the member 'ResolveClientUrl'.
Maybe I didn't understand what do you mean in your post. However, the above code runs well without any exceptions, and the method member gets invoked correctly, even it is internal to my assembly.
It may in some trust environments, but:
a) It won't in others
b) There's no guarantee that that method will be in future versions of
the framework
c) Internal methods should *not* be seen as part of the public
interface which you should call. If they were meant to be called by the
"outside world", they'd have been made protected or public.
Still, My puzzle is how to invoke inherited members using derived types? It seems that there's no difference in cases whether you have a BindingFlags.DeclareOnly specified or not:( Why is it so strange?
Well, just looking at your code again, you're using
BindingFlags.GetProperty, which probably isn't what you're after for a
method call.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet wrote: Well, just looking at your code again, you're using BindingFlags.GetProperty, which probably isn't what you're after for a method call.
So sorry for the mistake I made during copying code to this post!! The original
code snippet should be:
//...
MyControl instance = new MyControl();
Type type = instance.GetType();
object[] args = new object[] {"images/test.gif"};
string path = (string)type.InvokeMember("ResolveClientUrl",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.InvokeMethod,
null, instance, args); // Exception is thrown, withou no such member
found in the derived class.
//...
The reason why I made this mistake is actually I've wrote a helper class
as the following:
// code starts
using System;
using System.Reflection;
namespace LaserWeb.Presentation.Common
{
internal sealed class Reflector
{
private Reflector() {}
public static object GetProperty(object instance, string name)
{
Type type = instance.GetType();
return type.InvokeMember(name,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.GetProperty,
null, instance, null);
}
public static object GetProperty(Type type, object instance, string name)
{
return type.InvokeMember(name,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.GetProperty,
null, instance, null);
}
public static object InvokeMethod(object instance, string name, object[]
args)
{
Type type = instance.GetType();
return type.InvokeMember(name,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.InvokeMethod,
null, instance, args);
}
public static object InvokeMethod(Type type, object instance, string name,
object[] args)
{
return type.InvokeMember(name,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.InvokeMethod,
null, instance, args);
}
}
}
// code ends
When I posted the puzzle, the original code snippet is not handy (I have
removed it from my project), so I made the wrong code snippet by copying
the code fragment from Reflector.GetProperty() and modifing base on that
code. That's the reason why I made the mistake, sorry again for misleading
all of you in your reading!
If you use the above helper class as the following, you will still find the
problem I described:
// ..
MyControl instance = new MyControl();
object args = new objet[] {"images/test.gif"};
Reflector.InvokeMethod(typeof(System.Web.UI.Contro l), instance, "ResolveClientUrl",
args); // This will works fine.
Reflector.InvokeMethod(instance, "ResolveClientUrl", args); // An exception
will be thrown here, indicating member not found.
// ...
I don't know why I had encountered such a strange problem, and got puzzled.
You may make the similar sample code and have a try, to see whether what
I said would happen. Thanks:)
Laser Lu.
Laser Lu <la******@hotmail.com> wrote: Jon Skeet wrote: Well, just looking at your code again, you're using BindingFlags.GetProperty, which probably isn't what you're after for a method call. So sorry for the mistake I made during copying code to this post!! The original code snippet should be: //... MyControl instance = new MyControl(); Type type = instance.GetType(); object[] args = new object[] {"images/test.gif"}; string path = (string)type.InvokeMember("ResolveClientUrl", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, instance, args); // Exception is thrown, withou no such member found in the derived class. //...
<snip>
Okay, well I can reproduce the problem, but not solve it. However, as
I've said before, this isn't something you should be doing anyway. I
urge you to change your design so that you don't need to call internal
methods in other assemblies.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Okay, thank you very much!
And I will try to avoid invoking internal or private members in ther future,
as doing this will certainly violate the design principles:) Okay, well I can reproduce the problem, but not solve it. However, as I've said before, this isn't something you should be doing anyway. I urge you to change your design so that you don't need to call internal methods in other assemblies. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Laser Lu |
last post by:
HI, all,
I just want to invoke an internal method named 'ResolveClientUrl', which
is defined in class System.Web.UI.Control, using an instance object of a
type that derives from Control.
Let's...
|
by: xavier vazquez |
last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
|
by: xavier vazquez |
last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
|
by: ashish0799 |
last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM
Jigsaw puzzles. You would have solved many in your childhood and many people still
like it in their old ages also. Now what you have got to do...
|
by: oncue01 |
last post by:
Word Puzzle
Task
You are going to search M words in an N × N puzzle. The words may have
been placed in one of the four directions as from (i) left to right (E), (ii) right
to left (W), (iii) up...
|
by: Phoe6 |
last post by:
Hi All,
I would like to request a code and design review of one of my program.
n-puzzle.py
http://sarovar.org/snippet/detail.php?type=snippet&id=83
Its a N-puzzle problem solver ( Wikipedia page...
|
by: PC GROUP |
last post by:
C#, VS2005, THREADS
Ok.
I have to ways to achieve (almost) the same thing.
What do i want to do? Control the progress of an operation in a different project of my application.
...
|
by: Gio |
last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book?
And while I'm there, should I get the Puzzle Book? Or should I save the
Puzzle Book for when I'm more advanced?
- Gio
...
|
by: honey777 |
last post by:
Problem: 15 Puzzle
This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle:
The goal is to...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |