473,734 Members | 6,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unexpected behaviour reflection & params

Consider the following method:
TOTAL(string v, params Object[] c)

Called with the following values :
A, {B, {C, {D,E},F}

or (as a 'structured' array layout) :

A,
[0] B
[1] ----- [0] C
----- [1] ------ [0] D
------ [1] E
----- [2] F

When this method gets called (via reflection) for the first time. The
value of 'c' is:

[0] --- [0] B
--- [1] ---- [0] C ...etc.etc.

But subsequent calls (with _exaclty_ the same values) result in a value
of 'c' of:

[0] B
[1] ---- [0] C ...etc. etc.

The method (TOTAL) is also called between the calls with the same set
of values.
I miss one level! Is this by design or am I missing something? If it is
by design, what's the use of doing it this way?

Dec 5 '05 #1
5 1292
papa.c...@gmail .com wrote:
Consider the following method:
TOTAL(string v, params Object[] c)

Called with the following values :
A, {B, {C, {D,E},F}

or (as a 'structured' array layout) :


<snip>

That certainly sounds odd. Could you post a short but complete program
which demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Dec 5 '05 #2
Made a mistake with the values; here are some actual runtime snapshots

Quick watch results on c on 1st call (tabbed):

- calls {Length=1} System.Object[]
- [0] {Length=3} string[]
[0] "B" string
- [1] {Length=2} string[]
[0] "C" string
[1] "D" string
[2] "E" string

Quick watch results on c on subsequent calls (tabbed):

- calls {Length=3} System.Object[]
[0] "B" string
- [1] {Length=2} string[]
[0] "C" string
[1] "D" string
[2] "E" string

Dec 5 '05 #3
The following code produces the same result:

using System;
using System.Reflecti on;

namespace RemoveMe
{
class Problem
{
private Object[] objPrm = new Object[]{"A",new Object[] {"B", new
Object[]{"C","D"} ,"E"}};

[STAThread]
static void Main(string[] args)
{
Problem p = new Problem();
p.Demonstrate() ;
}

public void Demonstrate()
{
this.Call();
this.Call();
}

public void Call()
{
Object retObj = this.GetType(). InvokeMember("T otal",
BindingFlags.In vokeMethod, null, this,this.objPr m);
}

public Object Total(string v, params Object[] c)
{
return null; // Breakpoint here
}
}
}

Dec 5 '05 #4
Papa.Coen wrote:
The following code produces the same result


<snip>

Thanks. (I modified it to print out the contents - it's helpful not to
have to run inside a debugger to see a problem.)

I suspect it's a .NET 1.1 bug - I only see it when compiling with the
1.1 compiler.
Very odd though... my guess is that the best response you'll get if you
raise it with MS is "it's fixed in 2.0" although I could be wrong. I
don't know how best to work around it though, other than to not use
params parameters...

Jon

Dec 5 '05 #5
Using .net 2.0 and not using params is not an option for me (now)
I work around it with the following statement:

if(c[0] is string)
{
// Process
}
else
{
foreach(Object[] call in c)
{
// Process
}
}

Not very elegant, I still want to know what the problem is.

Dec 5 '05 #6

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

Similar topics

3
10992
by: Richard | last post by:
I have written a static method that uses the Reflection API to scrape all of the names of the instance variables in a class and their corresponding values; and then returns a hashMap containing a key/value pair of the name of the variable and its corresponding value. My problem is getting the value of the field, and everything I have seen on usenet and in the javadoc tells me I am doing the right thing. I think I found a bug in the JDK,...
1
2258
by: Derek Hart | last post by:
I have the following code: Me.GetType().InvokeMember(FunctionToRun, BindingFlags.InvokeMethod, Nothing, Me, params) I can send in a string from a database into the FunctionToRun variable, and it will run the method in the current class, but I need to read parameters from a database into the params object, and this is where I am stuck. It seems that the params may need to be actual variables, but I am not sure. How can I take a list of...
2
2168
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of virtual method calls. To further complicate (or perhaps simplify) matters, some compilers (GCC and MingW) produce the expected behaviour, while others (MSVS 7.1) do not. I can only offer two explanations for my observations: 1. The Microsoft...
10
7366
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
3
4203
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in Fields) { Object Temp = fi.GetValue(Obj); if (Temp.GetType().IsArray)
2
2977
by: =?Utf-8?B?UmVuYXVkIExhbmdpcw==?= | last post by:
Hello, I have an asp.net web page (say page.aspx) which derives from a custom base page object (CustomPage : BasePage : System.Web.UI.Page) Which has a method called DoSomething(params). My web page contains a UserControl (say Control.ascx). In the control class, i want to call the DoSomething method. I cannot use type cast since in fact the base class Page structure is a little more complex (see bottom of post). Hence i cannot do:
2
2568
by: FFMG | last post by:
Hi, I was looking at http://www.php.net/manual/en/function.call-user-func-array.php and I was wondering... Given, // -- function foo( &$params) {
1
1427
by: Just Another Victim of the Ambient Morality | last post by:
HTMLParser is behaving in, what I find to be, strange ways and I would like to better understand what it is doing and why. First, it doesn't appear to translate HTML escape characters. I don't know the actual terminology but things like &amp; don't get translated into & as one would like. Furthermore, not only does HTMLParser not translate it properly, it seems to omit it altogether! This prevents me from even doing the translation...
3
3342
by: Giovanni Gherdovich | last post by:
Hello, in the following code I have a pointer (to function), say p, of type double (*)(double, double, void*) and I try to fix the second argument of the function *p to a given value (using boost::bind), but the compiler complains, because of a type mismatch in an assignment which I think should be legal:
0
8957
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
8783
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
9459
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
9193
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
8194
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
6043
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3268
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.