473,397 Members | 2,099 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,397 software developers and data experts.

Calling known method on unknown type?

I have a situation where I have multiple objects that aren't related in any way
(no base class), but all have a couple of common methods/properties. I'm
looking for a clean way to call a particular method/property for the object,
w/o having to resort to a bunch of if-is-cast statements.

Here is what I'd like to be able to do:

object source = GetSource();

object width = some_mystery_function(source, "Width");

Get what I'm after? I'm suspecting that reflection is what I need, but have no
experience w/ it, and would rather have some pointers before jumping into it.
Nov 16 '05 #1
6 2849
Julie wrote:
I have a situation where I have multiple objects that aren't related in any way
(no base class), but all have a couple of common methods/properties. I'm
looking for a clean way to call a particular method/property for the object,
w/o having to resort to a bunch of if-is-cast statements.
Well, usually this is what base classes are for.
Here is what I'd like to be able to do:

object source = GetSource();

object width = some_mystery_function(source, "Width");


It's fairly simple using Microsoft.VisualBasic.Interaction.CallByName()
but I suppose there is another way using Reflection to get around
VisualBasic namespace ...

--
Konrad -
http://madrat.net/
Nov 16 '05 #2
Julie wrote:

I have a situation where I have multiple objects that aren't related in any way
(no base class), but all have a couple of common methods/properties. I'm
looking for a clean way to call a particular method/property for the object,
w/o having to resort to a bunch of if-is-cast statements.

Here is what I'd like to be able to do:

object source = GetSource();

object width = some_mystery_function(source, "Width");

Get what I'm after? I'm suspecting that reflection is what I need, but have no
experience w/ it, and would rather have some pointers before jumping into it.


I was able to find a way to do what I'm after:

object source = GetSource();

Type myType = source.GetType();

System.Reflection.PropertyInfo myProperty = myType.GetProperty("Width");

object width = myProperty.GetValue(source, null);
Nov 16 '05 #3
Konrad and Julie,

You can use reflection (although using the Visual Basic namespace is not
that bad either). You can get the type of the object through the GetType
method on the object. Once you have that, call the GetMethod method on the
Type, getting the MethodInfo instance representing the method. Finally,
call the Invoke method, passing the instance to call it on, and the
parameters.

Konrad's suggestion of a base class is a good idea. However, if a base
class is not feasable, an interface would be a much better option.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Konrad L. M. Rudolph" <ko************@madrat.net> wrote in message
news:u8**************@TK2MSFTNGP10.phx.gbl...
Julie wrote:
I have a situation where I have multiple objects that aren't related in any way (no base class), but all have a couple of common methods/properties. I'm looking for a clean way to call a particular method/property for the object, w/o having to resort to a bunch of if-is-cast statements.


Well, usually this is what base classes are for.
Here is what I'd like to be able to do:

object source = GetSource();

object width = some_mystery_function(source, "Width");


It's fairly simple using Microsoft.VisualBasic.Interaction.CallByName()
but I suppose there is another way using Reflection to get around
VisualBasic namespace ...

--
Konrad -
http://madrat.net/

Nov 16 '05 #4
On Wed, 12 May 2004 10:42:30 -0700, Julie <ju***@nospam.com> wrote:
I have a situation where I have multiple objects that aren't related in any way
(no base class), but all have a couple of common methods/properties. I'm
looking for a clean way to call a particular method/property for the object,
w/o having to resort to a bunch of if-is-cast statements.
This is the whole point of interfaces. Define it like:

interface IProps
{
int Width
{
get;
set;
}

}

IProps iprops=(IProps)GetSource();
int width=iprops.Width;

Here is what I'd like to be able to do:

object source = GetSource();

object width = some_mystery_function(source, "Width");

Get what I'm after? I'm suspecting that reflection is what I need, but have no
experience w/ it, and would rather have some pointers before jumping into it.


Nov 16 '05 #5
was just about to say the same thing.

"Austin Ehlers" <th**************@hotmail.com> wrote in message
news:sa********************************@4ax.com...
On Wed, 12 May 2004 10:42:30 -0700, Julie <ju***@nospam.com> wrote:
I have a situation where I have multiple objects that aren't related in any way(no base class), but all have a couple of common methods/properties. I'm
looking for a clean way to call a particular method/property for the object,w/o having to resort to a bunch of if-is-cast statements.
This is the whole point of interfaces. Define it like:

interface IProps
{
int Width
{
get;
set;
}

}

IProps iprops=(IProps)GetSource();
int width=iprops.Width;

Here is what I'd like to be able to do:

object source = GetSource();

object width = some_mystery_function(source, "Width");

Get what I'm after? I'm suspecting that reflection is what I need, but have noexperience w/ it, and would rather have some pointers before jumping into

it.

Nov 16 '05 #6
James Morton wrote:

was just about to say the same thing.

"Austin Ehlers" <th**************@hotmail.com> wrote in message
news:sa********************************@4ax.com...
On Wed, 12 May 2004 10:42:30 -0700, Julie <ju***@nospam.com> wrote:
I have a situation where I have multiple objects that aren't related in any way(no base class), but all have a couple of common methods/properties. I'm
looking for a clean way to call a particular method/property for the object,w/o having to resort to a bunch of if-is-cast statements.


This is the whole point of interfaces. Define it like:

interface IProps
{
int Width
{
get;
set;
}

}

IProps iprops=(IProps)GetSource();
int width=iprops.Width;

Here is what I'd like to be able to do:

object source = GetSource();

object width = some_mystery_function(source, "Width");

Get what I'm after? I'm suspecting that reflection is what I need, but have noexperience w/ it, and would rather have some pointers before jumping into

it.


My question was *NOT* about interfaces or base classes, though.

As I mentioned, I found the answer to my question myself. Thanks to the others
that responded w/ relevant answers.
Nov 16 '05 #7

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

Similar topics

3
by: Bret Thompson | last post by:
I am attempting to write an ASP page that will download a file rather then open it, when it is of known type. I found this code here: http://mosley.arach.net.au/dev/docs/save%20as.htm The first...
10
by: jim.brown | last post by:
Please refer me to the right place if this is the wrong place to post this question. I'm looking for an example of calling the Eigenvalue routines of JAMA from a C++ program. The documentation...
7
by: Robert Oschler | last post by:
I'm having a very painful time converting some Mozilla dynamic DOM code to work with Internet Explore. For example, given this code: -------------- selectBox=document.createElement("SELECT");...
4
by: Calum | last post by:
I need to call a function in a shared library, but the type of the function is not known until run-time. enum Type { Void=0, Char, Uchar, Short, Ushort, Int, Uint, Float, Double, String }; ...
3
by: JB | last post by:
I have an aplication which used to be created in Delphi 5. For various reasons we want to try to recode that in vb .net but the original application called functions inside one or more dlls that...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
6
by: Mail.To.Nathaniel | last post by:
Hi all :) I have an-easy-to-resolve (I am sure but I am obviously unable to figure the answer out) problem for C++ gurus. Here's the situation I have fallen into... 2 header files: H1.h &...
0
by: Chris Bordeman | last post by:
Hi all. My WCF service accepts a base interface type I call 'IGenericRequest' as a *parameter*. I have been able to accomplish this using NetDataContractSerializer. But then it gets sticky...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...

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.