471,354 Members | 1,212 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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 1215
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.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
}
}
}

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Derek Hart | last post: by
2 posts views Thread by Gerhard Esterhuizen | last post: by
3 posts views Thread by trevorelbourne | last post: by
2 posts views Thread by =?Utf-8?B?UmVuYXVkIExhbmdpcw==?= | last post: by
1 post views Thread by Just Another Victim of the Ambient Morality | last post: by
3 posts views Thread by Giovanni Gherdovich | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.