473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Relection on interfaces

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
4 1954
<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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
5
by: jg | last post by:
I followed the pattern in the MSDN dot net example for .net server and I tried a making simple .net dll as COM from Option Explicit On Option Strict On Imports System Imports...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.