Calling known method on unknown type?

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.
 
K

Konrad L. M. Rudolph

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 ...
 
J

Julie

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);
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
A

Austin Ehlers

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;
 
J

James Morton

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.
 
J

Julie

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top