Calling known method on unknown type?

  • Thread starter Thread starter Julie
  • Start date Start date
J

Julie

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.
 
Julie said:
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 ...
 
Julie said:
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);
 
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.
 
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;
 
was just about to say the same thing.

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

interface IProps
{
int Width
{
get;
set;
}

}

IProps iprops=(IProps)GetSource();
int width=iprops.Width;
it.
 
James said:
was just about to say the same thing.

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.
 
Back
Top