By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Relection on interfaces

P: n/a
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.

Sep 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<ju*************@googlemail.coma écrit dans le message de news:
11**********************@d34g2000cwd.googlegroups. com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 22 '06 #2

P: n/a
ju*************@googlemail.com <ju*************@googlemail.comwrote:
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)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 22 '06 #3

P: n/a
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!

Joanna Carter [TeamB] wrote:
<ju*************@googlemail.coma écrit dans le message de news:
11**********************@d34g2000cwd.googlegroups. com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 26 '06 #4

P: n/a
<ju*************@googlemail.coma écrit dans le message de news:
11**********************@b28g2000cwb.googlegroups. com...

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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.