473,785 Members | 3,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_fu nction(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 2886
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_fu nction(source, "Width");


It's fairly simple using Microsoft.Visua lBasic.Interact ion.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_fu nction(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.Reflecti on.PropertyInfo myProperty = myType.GetPrope rty("Width");

object width = myProperty.GetV alue(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.co m

"Konrad L. M. Rudolph" <ko************ @madrat.net> wrote in message
news:u8******** ******@TK2MSFTN GP10.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_fu nction(source, "Width");


It's fairly simple using Microsoft.Visua lBasic.Interact ion.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.c om> 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.Wi dth;

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

object source = GetSource();

object width = some_mystery_fu nction(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.c om...
On Wed, 12 May 2004 10:42:30 -0700, Julie <ju***@nospam.c om> 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.Wi dth;

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

object source = GetSource();

object width = some_mystery_fu nction(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.c om...
On Wed, 12 May 2004 10:42:30 -0700, Julie <ju***@nospam.c om> 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.Wi dth;

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

object source = GetSource();

object width = some_mystery_fu nction(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
4518
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 example works fine, for files under 5 Mb, the second example downloads the file but the data is corrupted. Using a JGP file as a reference, it seems to corrupt the end of the file (since the bottom of the JPEG is corrupted). Here is the code...
10
3214
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 says that the is the public method: Eigenvalue (const TNT::Array2D< Real > &A) I write this program
7
3146
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"); selectBox.name="theSelectBox"; optionOne=document.createElement("OPTION"); optionOne.name="option1"; optionOne.value="one";
4
3739
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 }; void call_unknown_function( void *function, enum Type retType, void *retAddress, ...);
3
2158
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 were loaded dynaically by the name found at run time. i.e. what would happen is that the application would start, it finds all the *.dll functions available in a specified subdirectory. The user would then select one target platform which...
18
4359
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
6
1677
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 & H2.h -------------------------------------------------------------------- --- H1.h
0
1345
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 because the client will actually be sending in an object of some other class that implements 'IGenericRequest.' Normally this is easily conquered by adding a known type, but... The hangup is the derived type will be totally unknown to the WCF
7
2688
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.