Relection on interfaces

J

jupitermoonbeam

Hello,

I wondered if anyone could help. I wish to retrieve the PropertyInfo[]
of a specific interface but can't work out how to do this in a type
safe way.

Basically I don't want to do this:
void Reflect(IMyObject objectToReflect)
{
PropertyInfo[] myObjectProperties =
objectToReflect.GetType().GetProperties();
}

Reason being it may have multiple interfaces or it's own properties and
the reflection is based on the object not on the parameter type (i.e.
GetType() returns will return object type which inherits the interface
not the interface itself).

So what I'm trying to do is this instead:
void Reflect(IMyObject objectToReflect)
{
PropertyInfo[] myObjectProperties =
Type.GetType(IMyObject).GetProperties();
}

But I can't find the code to do it (unless I use a string - but then
that's not type safe).

Anyone have any ideas?

Most grateful.
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| So what I'm trying to do is this instead:
| void Reflect(IMyObject objectToReflect)
| {
| PropertyInfo[] myObjectProperties =
| Type.GetType(IMyObject).GetProperties();
| }

Try this :

void Reflect(IMyObject objectToReflect)
{
PropertyInfo[] myObjectProperties = typeof(IMyObject).GetProperties();
}

Joanna
 
J

Jon Skeet [C# MVP]

I wondered if anyone could help. I wish to retrieve the PropertyInfo[]
of a specific interface but can't work out how to do this in a type
safe way.

Basically I don't want to do this:
void Reflect(IMyObject objectToReflect)
{
PropertyInfo[] myObjectProperties =
objectToReflect.GetType().GetProperties();
}

Reason being it may have multiple interfaces or it's own properties and
the reflection is based on the object not on the parameter type (i.e.
GetType() returns will return object type which inherits the interface
not the interface itself).

So what I'm trying to do is this instead:
void Reflect(IMyObject objectToReflect)
{
PropertyInfo[] myObjectProperties =
Type.GetType(IMyObject).GetProperties();
}

But I can't find the code to do it (unless I use a string - but then
that's not type safe).

I think what you're after is:

void Reflect (Type interfaceType, object targetObject)
{
PropertyInfo[] properties = interfaceType.GetProperties();
// use properties with targetObject
}

Then you'd use it with:

Reflect (typeof(IMyObject), myObject)
 
J

jupitermoonbeam

Thanks very much.

I know it sounds stupid but I had one of those typical developer "I
swear I tried that and it didn't work". Not sure how I missed it but
thanks for the help!
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

I know it sounds stupid but I had one of those typical developer "I
swear I tried that and it didn't work". Not sure how I missed it but
thanks for the help!

No problem, I've just about printed the tee-shirt on that one :)

Joanna
 

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