473,413 Members | 1,802 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,413 software developers and data experts.

Finding method by signature at runtime

Let's say there is an object which has several methods all taking
different object as argument:

class TestObject {

public void SetTitle( TitleObject title );
public void SetDate( DateObject date );
<etc.>
}

How can you find out which method to call at runtime when you have an
object which should be passed to a method? I suppose there is a cleaner
way to do it than by testing all possibilities by a set of if-clauses:

public void SetNull( Object o ) {

if ( o is TitleObject ) {
SetTitle(null);
} else
if ( o is DateObject ) {
SetDate(null);
} else
<etc.>

}

With best regards
Pekka
Kerava, Finland

Nov 17 '05 #1
4 1392
You don't actually need to do that. The compiler can do that for you if you
just overload a single "Set" method:

public void Set(TitleObject title) {
_title = title;
}
public void Set(DateObject date) {
_date = date;
}
// etc...

Because the method signatures differ, the compiler can figure out which one
you want at compile time based on the type you're passing in. Sadly, the
same trick doesn't work quite as cleanly for a "Get" function, because
return type is not part of a method's formal signature. The best you can do
on the flip side is to implement a generic "get" like this:

public object Get(string fieldname) {
switch(fieldname) {
case "title": return (object)_title;
case "date": return (object)_date;
default: return (object)null;
}
}

This works fine, but requires the caller to cast the result to the desired
type, which you can't necessarily trust them to do correctly.

--
Anything I post is solely my opinion, and is not necessarily representative
of my employer's positons.
My answers are not always correct, but I do my best to be accurate and
helpful.
"Pekka Henttonen" <no********@kolumbus.fi> wrote in message
news:MP************************@news.kolumbus.fi.. .
Let's say there is an object which has several methods all taking
different object as argument:

class TestObject {

public void SetTitle( TitleObject title );
public void SetDate( DateObject date );
<etc.>
}

How can you find out which method to call at runtime when you have an
object which should be passed to a method? I suppose there is a cleaner
way to do it than by testing all possibilities by a set of if-clauses:

public void SetNull( Object o ) {

if ( o is TitleObject ) {
SetTitle(null);
} else
if ( o is DateObject ) {
SetDate(null);
} else
<etc.>

}

With best regards
Pekka
Kerava, Finland

Nov 17 '05 #2
P.S. If the code that's calling your overloaded Set() method actually has a
TitleObject or a DateObject at hand, then my approach works fine. Upon
re-reading your question, though, that isn't clearly stated. If the caller
merely has a generic object, and it doesn't know what derived type the
object is either, then I'm hard-pressed to think of a solution that doesn't
involve reflection. But if the caller doesn't know what kind of thing it's
trying to set either, then your program may have bigger problems than
this...

--
Anything I post is solely my opinion, and is not necessarily representative
of my employer's positons.
My answers are not always correct, but I do my best to be accurate and
helpful.
"Jason Black [MSFT]" <ja*****@microsoft.com> wrote in message
news:42********@news.microsoft.com...
You don't actually need to do that. The compiler can do that for you if
you just overload a single "Set" method:

public void Set(TitleObject title) {
_title = title;
}
public void Set(DateObject date) {
_date = date;
}
// etc...

Because the method signatures differ, the compiler can figure out which
one you want at compile time based on the type you're passing in. Sadly,
the same trick doesn't work quite as cleanly for a "Get" function, because
return type is not part of a method's formal signature. The best you can
do on the flip side is to implement a generic "get" like this:

public object Get(string fieldname) {
switch(fieldname) {
case "title": return (object)_title;
case "date": return (object)_date;
default: return (object)null;
}
}

This works fine, but requires the caller to cast the result to the desired
type, which you can't necessarily trust them to do correctly.

--
Anything I post is solely my opinion, and is not necessarily
representative of my employer's positons.
My answers are not always correct, but I do my best to be accurate and
helpful.
"Pekka Henttonen" <no********@kolumbus.fi> wrote in message
news:MP************************@news.kolumbus.fi.. .
Let's say there is an object which has several methods all taking
different object as argument:

class TestObject {

public void SetTitle( TitleObject title );
public void SetDate( DateObject date );
<etc.>
}

How can you find out which method to call at runtime when you have an
object which should be passed to a method? I suppose there is a cleaner
way to do it than by testing all possibilities by a set of if-clauses:

public void SetNull( Object o ) {

if ( o is TitleObject ) {
SetTitle(null);
} else
if ( o is DateObject ) {
SetDate(null);
} else
<etc.>

}

With best regards
Pekka
Kerava, Finland


Nov 17 '05 #3
In article <42********@news.microsoft.com>, ja*****@microsoft.com
says...
You don't actually need to do that. The compiler can do that for you
if you just overload a single "Set" method:
Thanks, I know that. It would be the easiest way to approach the
question. But I am not sure how it fits into the bigger picture. I must
give it a thought.

I suppose also null objects have a type? Would the compiler know what
method should be called if the object (in the example DateObject or
TitleObject) passed is null?
P.S. If the code that's calling your overloaded Set() method actually
has a TitleObject or a DateObject at hand, then my approach works fine.
Upon re-reading your question, though, that isn't clearly stated. If
the caller merely has a generic object, and it doesn't know what
derived type the object is either, then I'm hard-pressed to think of a
solution that doesn't involve reflection.


The program knows that it is dealing with an object derived from a base
class but not exactly what type of object it is. There is no serious
problem :-)

I thought that the reflection might also do the trick, but I do not know
enough about it to use it.

With best regards
Pekka
Nov 17 '05 #4
Pekka Henttonen <no********@kolumbus.fi> wrote:
You don't actually need to do that. The compiler can do that for you
if you just overload a single "Set" method:


Thanks, I know that. It would be the easiest way to approach the
question. But I am not sure how it fits into the bigger picture. I must
give it a thought.

I suppose also null objects have a type? Would the compiler know what
method should be called if the object (in the example DateObject or
TitleObject) passed is null?


There's no such thing as a null object. However, the compiler resolves
overloads at compile time, not at runtime. It looks at the type of the
expression and decides which method to call based on that.

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

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

Similar topics

19
by: Dave Raskin | last post by:
public class Base { } public class Derived : Base { } public class Service {
8
by: AngryGerbil | last post by:
hey, How do I acquire MethodInfo WITHOUT hardcoding method name as a string?!??!The fact I have to use xxx.Gettype.GetMethod("MyMethod", xxx) is making me want to drive an ice pick into my eye! I...
3
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
10
by: dotnetchic | last post by:
Anybody know what the "internal limitation" of a method signature is for c++/cli? I've got a mixed assembly that throws an exception for one particular function (very large if-else control). It...
2
by: chris | last post by:
I am sure I am overlooking something simple, but here goes. In the global.asax file I have some code in the Application_Error method that reads information out of the current exception and emails...
4
by: Alan T | last post by:
I got a method in my ancestor form declared as Protected, this method has empty body. In my descendant form I declared as Protected also, then compile has no problem but the name of the method has...
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
by: creo | last post by:
OS: Windows XP (32 bit) NET: .Net 2.0 runtime DevEnv: VS 2008 C++ and C# I am attempting to use an unmanaged C++ library to load a managed C# assembly and hand off control. I have the following...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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,...
0
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...
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
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,...
0
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...

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.