473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Inconsistant null parameter handling in Reflection

Hi,

It seems that the way reflection resolves methods is not quite the same as
default CLR. For example this simple class:

class Test {
public void Hello(string name) {
Console.WriteLine("Hello(string)");
}
public void Hello(object obj) {
Console.WriteLine("Hello(object)");
}
}

If I invoke the method normally using null parameter:

Test test = new Test();
test.Hello(null); // shows Hello(string)

But if I only know the input parameter (which is null) and the name of the
method, and trying to use reflection to resolve the method to invoke, it
becomes:

Test test = new Test();
MethodInfo method = test.GetType().GetMethod("Hello", new Type[] {
typeof(void) });
method.invoke(test, new object[] { null }); // shows Hello(object)

The result changed. It must because of the typeof(void) thing, but I can't
say typeof(string) because the input parameter is null. What should I do to
get the same result?
Thanks,
Stefan
Nov 16 '05 #1
16 3723
Stefan Hong <st****@mosp.net> wrote:
It seems that the way reflection resolves methods is not quite the same as
default CLR.
It's not the CLR that decides which overload to call in the statically
compiled case - it's the C# compiler.

<snip>
But if I only know the input parameter (which is null) and the name of the
method, and trying to use reflection to resolve the method to invoke, it
becomes:

Test test = new Test();
MethodInfo method = test.GetType().GetMethod("Hello", new Type[] {
typeof(void) });
method.invoke(test, new object[] { null }); // shows Hello(object)

The result changed. It must because of the typeof(void) thing, but I can't
say typeof(string) because the input parameter is null. What should I do to
get the same result?


You can still use typeof(string) even if the input parameter is null.
(I'm surprised you can get away with typeof(void) in the above, to be
honest.)

If you only receive null and you basically don't know which method to
call, but you want to call the most specific method, you'll need to
call Type.GetMethods and work out which one to call for yourself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Stefan Hong <st****@mosp.net> wrote:
It seems that the way reflection resolves methods is not quite the same
as
default CLR.
It's not the CLR that decides which overload to call in the statically
compiled case - it's the C# compiler.


<snip>

You can still use typeof(string) even if the input parameter is null.
(I'm surprised you can get away with typeof(void) in the above, to be
honest.)

If you only receive null and you basically don't know which method to
call, but you want to call the most specific method, you'll need to
call Type.GetMethods and work out which one to call for yourself.


Hmm... to completely mimic the type binding rule used by C# compiler seems
not easy and error-prone, is there a such Binder class exists in the .net
framework library already?
Thanks,
Stefan
Nov 16 '05 #3
Stefan Hong <st****@mosp.net> wrote:
If you only receive null and you basically don't know which method to
call, but you want to call the most specific method, you'll need to
call Type.GetMethods and work out which one to call for yourself.


Hmm... to completely mimic the type binding rule used by C# compiler seems
not easy and error-prone, is there a such Binder class exists in the .net
framework library already?


I don't believe so, I'm afraid. I would try to avoid it, personally,
but it's hard to know exactly what that entails without having more
information about your problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi,
Test test = new Test();
MethodInfo method = test.GetType().GetMethod("Hello", new Type[] {
typeof(void) });
method.invoke(test, new object[] { null }); // shows Hello(object)


You should try

test.GetType().InvokeMember(
"Hello", BindingFlags.Public, null, test, new object[] {null}
);

The default binder will be applied and I'd bet, that it
will dispatch to the correct method.

bye
Rob
Nov 16 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Stefan Hong <st****@mosp.net> wrote:
> If you only receive null and you basically don't know which method to
> call, but you want to call the most specific method, you'll need to
> call Type.GetMethods and work out which one to call for yourself.


Hmm... to completely mimic the type binding rule used by C# compiler
seems
not easy and error-prone, is there a such Binder class exists in the .net
framework library already?


I don't believe so, I'm afraid. I would try to avoid it, personally,
but it's hard to know exactly what that entails without having more
information about your problem.


I am experimenting a new remote invocation protocol, similar to what web
services can do but is a completely different thing. I can unwrap the
invocation request at the remote site to get the name of the method and an
array of unmarshalled parameter objects, but then I can not find the right
method to invoke, especially when there is null among input parameters.
Stefan
Nov 16 '05 #6
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
Stefan Hong <st****@mosp.net> wrote:
> If you only receive null and you basically don't know which method to
> call, but you want to call the most specific method, you'll need to
> call Type.GetMethods and work out which one to call for yourself.


Hmm... to completely mimic the type binding rule used by C# compiler
seems
not easy and error-prone, is there a such Binder class exists in the .net
framework library already?


I don't believe so, I'm afraid. I would try to avoid it, personally,
but it's hard to know exactly what that entails without having more
information about your problem.


I am experimenting a new remote invocation protocol, similar to what web
services can do but is a completely different thing. I can unwrap the
invocation request at the remote site to get the name of the method and an
array of unmarshalled parameter objects, but then I can not find the right
method to invoke, especially when there is null among input parameters.
Stefan
Nov 16 '05 #7

"Robert Jordan" <ro*****@gmx.net> wrote:
Hi,
You should try

test.GetType().InvokeMember(
"Hello", BindingFlags.Public, null, test, new object[] {null}
);

The default binder will be applied and I'd bet, that it
will dispatch to the correct method.


I tried do this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod,
null, test, new object[] {null});

But I got a MissingMethodException says:

"Attempted to access a missing member"

Any idea?
Thanks,
Stefan
Nov 16 '05 #8

"Robert Jordan" <ro*****@gmx.net> wrote:
Hi,
You should try

test.GetType().InvokeMember(
"Hello", BindingFlags.Public, null, test, new object[] {null}
);

The default binder will be applied and I'd bet, that it
will dispatch to the correct method.


I tried do this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod,
null, test, new object[] {null});

But I got a MissingMethodException says:

"Attempted to access a missing member"

Any idea?
Thanks,
Stefan
Nov 16 '05 #9

"Stefan Hong" <st****@mosp.net> wrote:
I tried do this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod,
null, test, new object[] {null});

But I got a MissingMethodException says:

"Attempted to access a missing member"

Any idea?


I've played with the BindingFlags and now I can invoke the correct method
with this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
null, test, new object[] {null});

I tried to apply the same thing to test.GetType().GetMethod( ... ) but
wasn't able to get the same result. I must get MethodInfo before invoking
'cause I want to check some custom attributes first.

This could be a design flaw of Reflection because there is no way to specify
the type of null reference. I thought typeof(void) could do the trick but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.
Stefan
Nov 16 '05 #10

"Stefan Hong" <st****@mosp.net> wrote:
I tried do this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod,
null, test, new object[] {null});

But I got a MissingMethodException says:

"Attempted to access a missing member"

Any idea?


I've played with the BindingFlags and now I can invoke the correct method
with this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
null, test, new object[] {null});

I tried to apply the same thing to test.GetType().GetMethod( ... ) but
wasn't able to get the same result. I must get MethodInfo before invoking
'cause I want to check some custom attributes first.

This could be a design flaw of Reflection because there is no way to specify
the type of null reference. I thought typeof(void) could do the trick but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.
Stefan
Nov 16 '05 #11
Stefan Hong <st****@mosp.net> wrote:
I've played with the BindingFlags and now I can invoke the correct method
with this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
null, test, new object[] {null});

I tried to apply the same thing to test.GetType().GetMethod( ... ) but
wasn't able to get the same result. I must get MethodInfo before invoking
'cause I want to check some custom attributes first.

This could be a design flaw of Reflection because there is no way to specify
the type of null reference. I thought typeof(void) could do the trick but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.


No, because you don't *want* a typeof(null) or typeof(void). You want
to say what the declared parameter type is - eg typeof(string).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
Stefan Hong <st****@mosp.net> wrote:
I've played with the BindingFlags and now I can invoke the correct method
with this:

test.GetType().InvokeMember(
"Hello",
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
null, test, new object[] {null});

I tried to apply the same thing to test.GetType().GetMethod( ... ) but
wasn't able to get the same result. I must get MethodInfo before invoking
'cause I want to check some custom attributes first.

This could be a design flaw of Reflection because there is no way to specify
the type of null reference. I thought typeof(void) could do the trick but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.


No, because you don't *want* a typeof(null) or typeof(void). You want
to say what the declared parameter type is - eg typeof(string).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote;
Stefan Hong <st****@mosp.net> wrote:

This could be a design flaw of Reflection because there is no way to
specify
the type of null reference. I thought typeof(void) could do the trick
but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.


No, because you don't *want* a typeof(null) or typeof(void). You want
to say what the declared parameter type is - eg typeof(string).


If I already know the declared parameter type is string, I would definitely
use typeof(string). But in my case I don't. All I know is the name of the
method and the parameter objects. Default binder can resolve method without
any problem, so we can do:

test.Hello( null );

The null is just nothing, no declared type. Binder knows how to escalate
null to the most specific type. The problem is when we want to resolve the
MethodInfo using the same binding rule, there is no way to tell binder that
we have a null parameter to let binder do the escalation magic. That's why
I think a pseudo Type.Null or typeof(null) might be necessary.
Stefan
Nov 16 '05 #14
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote;
Stefan Hong <st****@mosp.net> wrote:

This could be a design flaw of Reflection because there is no way to
specify
the type of null reference. I thought typeof(void) could do the trick
but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.


No, because you don't *want* a typeof(null) or typeof(void). You want
to say what the declared parameter type is - eg typeof(string).


If I already know the declared parameter type is string, I would definitely
use typeof(string). But in my case I don't. All I know is the name of the
method and the parameter objects. Default binder can resolve method without
any problem, so we can do:

test.Hello( null );

The null is just nothing, no declared type. Binder knows how to escalate
null to the most specific type. The problem is when we want to resolve the
MethodInfo using the same binding rule, there is no way to tell binder that
we have a null parameter to let binder do the escalation magic. That's why
I think a pseudo Type.Null or typeof(null) might be necessary.
Stefan
Nov 16 '05 #15
Stefan Hong <st****@mosp.net> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote;
Stefan Hong <st****@mosp.net> wrote:

This could be a design flaw of Reflection because there is no way to
specify
the type of null reference. I thought typeof(void) could do the trick
but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.


No, because you don't *want* a typeof(null) or typeof(void). You want
to say what the declared parameter type is - eg typeof(string).


If I already know the declared parameter type is string, I would definitely
use typeof(string). But in my case I don't. All I know is the name of the
method and the parameter objects. Default binder can resolve method without
any problem, so we can do:

test.Hello( null );

The null is just nothing, no declared type. Binder knows how to escalate
null to the most specific type. The problem is when we want to resolve the
MethodInfo using the same binding rule, there is no way to tell binder that
we have a null parameter to let binder do the escalation magic. That's why
I think a pseudo Type.Null or typeof(null) might be necessary.


In the above, it's not the Binder at all - it's the C# overload
resolution rules. Type.Null or typeof(null) wouldn't help at all,
because there's no such thing as the null type - and certainly no
methods would be matching it. I think I see what you mean though.

I've tried getting Type.DefaultBinder.BindToMethod to work, but I
haven't managed so far...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #16
Stefan Hong <st****@mosp.net> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote;
Stefan Hong <st****@mosp.net> wrote:

This could be a design flaw of Reflection because there is no way to
specify
the type of null reference. I thought typeof(void) could do the trick
but
it couldn't. Maybe we need something like typeof(null) supported by C#
compiler or Type.Null in the framework.


No, because you don't *want* a typeof(null) or typeof(void). You want
to say what the declared parameter type is - eg typeof(string).


If I already know the declared parameter type is string, I would definitely
use typeof(string). But in my case I don't. All I know is the name of the
method and the parameter objects. Default binder can resolve method without
any problem, so we can do:

test.Hello( null );

The null is just nothing, no declared type. Binder knows how to escalate
null to the most specific type. The problem is when we want to resolve the
MethodInfo using the same binding rule, there is no way to tell binder that
we have a null parameter to let binder do the escalation magic. That's why
I think a pseudo Type.Null or typeof(null) might be necessary.


In the above, it's not the Binder at all - it's the C# overload
resolution rules. Type.Null or typeof(null) wouldn't help at all,
because there's no such thing as the null type - and certainly no
methods would be matching it. I think I see what you mean though.

I've tried getting Type.DefaultBinder.BindToMethod to work, but I
haven't managed so far...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #17

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

Similar topics

2
by: Marcin | last post by:
Hello! Is there any method to detect parameters values passed to called method? For example: public Guid ApplicationLogin(string userName, string password, int dbId)
4
by: James Fisher | last post by:
How are people handling NULL values for value types. Descriptions of problem Say you have a typical method with the signature: public int InsertPerson(string firstName, string lastName, int...
9
by: Bob | last post by:
I have a sql command with a SearchDate parameter of type date. When I execute its stored procedure from Query Analyzer (to SS2K) with a date out of the valid datetime range, EXEC @SearchDate =...
1
by: codejunkie | last post by:
Hi, I am running a suite of tests of a WIN 32 application. Every test launches the same application, checks if the Application has a Main Window handle and then proceeds. However i am noticing...
10
by: syntego | last post by:
I think I have discovered a bug in the handling of null values (vs NULL values) passed as parameters to a stored proc. I have always believed that the database handled NULL and null the same. ...
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
5
by: Arun Srinivasan | last post by:
I recently noticed the 'Database is consistant' parameter of the db cfg changed from Yes to No, when I did a 'set integrity immediate unchecked' command. the integrity of the table was...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.