473,659 Members | 3,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Reflection Puzzle:(

HI, all,
I just want to invoke an internal method named 'ResolveClientU rl', which
is defined in class System.Web.UI.C ontrol, 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.C ontrol):
//...
MyControl instance = new MyControl();
Type type = instance.GetTyp e();
object[] args = new object[] {"images/test.gif"};
string path = (string)type.In vokeMember("Res olveClientUrl",
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance |
BindingFlags.Ge tProperty,
null, instance, args);
//...

Actually, I intended to use the type object of MyControl to invoke the inherited
method 'ResolveClientU rl', but an Exception occured, described as "Method
LaserWeb.Presen tation.MyContro l.ResolveClient Url not found." It seems that
reflection didn't search in the inherited members up the class hierarchy,
since 'ResolveClientU rl' is a declared method in the base class Control.

This action, I guess, should only be taken when BindingFlags.De claredOnly
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.De claredOnly flag. Then why? How could
it happen? I was puzzled:( Help!

Regards,
Laser Lu
Jul 21 '05 #1
6 2837
Laser Lu <la******@hotma il.com> wrote:
HI, all,
I just want to invoke an internal method named 'ResolveClientU rl', which
is defined in class System.Web.UI.C ontrol, 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.C ontrol):
//...
MyControl instance = new MyControl();
Type type = instance.GetTyp e();
object[] args = new object[] {"images/test.gif"};
string path = (string)type.In vokeMember("Res olveClientUrl",
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance |
BindingFlags.Ge tProperty,
null, instance, args);
//...

Actually, I intended to use the type object of MyControl to invoke the inherited
method 'ResolveClientU rl', but an Exception occured, described as "Method
LaserWeb.Presen tation.MyContro l.ResolveClient Url not found." It seems that
reflection didn't search in the inherited members up the class hierarchy,
since 'ResolveClientU rl' is a declared method in the base class Control.

This action, I guess, should only be taken when BindingFlags.De claredOnly
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.De claredOnly 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Hello Jon Skeet, actually, the following code runs well:
//...
MyControl instance = new MyControl();
Type type = typeof(System.W eb.UI.Control); // here use the Type object of
Control, not of MyControl.
object[] args = new object[] {"images/test.gif"};
string path = (string)type.In vokeMember("Res olveClientUrl",
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.Ge tProperty,
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
'ResolveClientU rl'.

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.De clareOnly
specified or not:( Why is it so strange?
Laser Lu <la******@hotma il.com> wrote:
HI, all,
I just want to invoke an internal method named 'ResolveClientU rl',
which
is defined in class System.Web.UI.C ontrol, 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.C ontrol):
//...
MyControl instance = new MyControl();
Type type = instance.GetTyp e();
object[] args = new object[] {"images/test.gif"};
string path = (string)type.In vokeMember("Res olveClientUrl",
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
|
BindingFlags.Ge tProperty,
null, instance, args);
//...
Actually, I intended to use the type object of MyControl to invoke
the inherited method 'ResolveClientU rl', but an Exception occured,
described as "Method LaserWeb.Presen tation.MyContro l.ResolveClient Url
not found." It seems that reflection didn't search in the inherited
members up the class hierarchy, since 'ResolveClientU rl' is a
declared method in the base class Control.

This action, I guess, should only be taken when
BindingFlags.De claredOnly 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.De claredOnly 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.


Jul 21 '05 #3
Laser Lu <la******@hotma il.com> wrote:
Hello Jon Skeet, actually, the following code runs well:
//...
MyControl instance = new MyControl();
Type type = typeof(System.W eb.UI.Control); // here use the Type object of
Control, not of MyControl.
object[] args = new object[] {"images/test.gif"};
string path = (string)type.In vokeMember("Res olveClientUrl",
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.Ge tProperty,
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
'ResolveClientU rl'.

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.De clareOnly specified or not:( Why is it so strange?


Well, just looking at your code again, you're using
BindingFlags.Ge tProperty, which probably isn't what you're after for a
method call.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Jon Skeet wrote:
Well, just looking at your code again, you're using
BindingFlags.Ge tProperty, 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.GetTyp e();
object[] args = new object[] {"images/test.gif"};
string path = (string)type.In vokeMember("Res olveClientUrl",
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.In vokeMethod,
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.Reflecti on;

namespace LaserWeb.Presen tation.Common
{
internal sealed class Reflector
{
private Reflector() {}

public static object GetProperty(obj ect instance, string name)
{
Type type = instance.GetTyp e();
return type.InvokeMemb er(name,
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.Ge tProperty,
null, instance, null);
}

public static object GetProperty(Typ e type, object instance, string name)
{
return type.InvokeMemb er(name,
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.Ge tProperty,
null, instance, null);
}

public static object InvokeMethod(ob ject instance, string name, object[]
args)
{
Type type = instance.GetTyp e();
return type.InvokeMemb er(name,
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.In vokeMethod,
null, instance, args);
}

public static object InvokeMethod(Ty pe type, object instance, string name,
object[] args)
{
return type.InvokeMemb er(name,
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.In vokeMethod,
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.GetPr operty() 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.Invok eMethod(typeof( System.Web.UI.C ontrol), instance, "ResolveClientU rl",
args); // This will works fine.
Reflector.Invok eMethod(instanc e, "ResolveClientU rl", 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.
Jul 21 '05 #5
Laser Lu <la******@hotma il.com> wrote:
Jon Skeet wrote:
Well, just looking at your code again, you're using
BindingFlags.Ge tProperty, 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.GetTyp e();
object[] args = new object[] {"images/test.gif"};
string path = (string)type.In vokeMember("Res olveClientUrl",
BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
| BindingFlags.In vokeMethod,
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
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.


Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1470
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 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();
1
13088
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 the other....how i can fix this the program look like this import java.util.ArrayList; import java.util.Random;
0
2013
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 other....how i can fix this this run the random words for the program import javax.swing.JOptionPane; import java.util.ArrayList; import java.util.Random; public class CrossWordPuzzleTester {
5
4460
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 is to solve jigsaw puzzles using the computer. The jigsaw puzzle here is a square of dimension d (a puzzle with d^2 pieces) and the jigsaw pieces (all same dimensions) are of dimensions H x W (Which means the pieces have ‘H’ rows of ‘W’...
3
3201
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 to bottom (S), or (iv) bottom to up (N). The program will print the starting place and the direction of each word. Limitations The number of words to be searched can be at most 100, the size of the puzzle N can be minimum 5 maximum 20....
6
2571
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 and http://norvig.com/ltd/test/n-puzzle.lisp ) I have used OO Python for the above program and would like comments on my approach as I am just starting with OOP.
0
1035
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. Explanation: I have a winApp form. From there, using a static method from a Class Library project (in the same solution), I return a ReportDataSource object.
2
2694
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 -- AND NOW FOR A WORD (an IF blog):
4
19939
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 get the tiles in order, 1 through 15, from left to right, top to bottom, by just sliding tiles into the empty square. In this configuration, the goal would be to get the 14 and 15 to switch places, without affecting any of the other squares. Your...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.