Unexpected behaviour reflection & params

  • Thread starter Thread starter papa.coen
  • Start date Start date
P

papa.coen

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?
 
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
 
The following code produces the same result:

using System;
using System.Reflection;

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("Total",
BindingFlags.InvokeMethod, null, this,this.objPrm);
}

public Object Total(string v, params Object[] c)
{
return null; // Breakpoint here
}
}
}
 
Papa.Coen said:
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
 
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.
 
Back
Top