Get properties on interface (runtime/reflection)

A

Anderskj

Hi!

I am developing a c# application. I have a interface (which can change
therefore my problem)

If i do like this:

List<PropertyInfo> properties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetProperties());

I get the correct properties on the interface (9 pcs)

Now i want to dynamically load the interface by textstring (comming from
config file) and do like this.

Type viewInterface = Type.GetType("app.IView");

properties.AddRange(typeof(app.IView).GetProperties());

I now get 57 properties!. These i guess comes from the Type class.

How do i Achive my goal to load the interface by textstring name and the get
the properties on the interface?

Thanks in regards
Anders Jacobsen, Denmark
 
D

Dave Sexton

Hi,
I am developing a c# application. I have a interface (which can change
therefore my problem)

Your problem, I believe, is that your interface can change. You should
think about designing the interface so that it won't have to.
If i do like this:

List<PropertyInfo> properties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetProperties());

I get the correct properties on the interface (9 pcs)

Now i want to dynamically load the interface by textstring (comming from
config file) and do like this.

Type viewInterface = Type.GetType("app.IView");

properties.AddRange(typeof(app.IView).GetProperties());

I now get 57 properties!. These i guess comes from the Type class.

How do i Achive my goal to load the interface by textstring name and the
get the properties on the interface?

I suspect that the properties list already contains some items (48 to be
exact :). Try clearing it before you fill it: properties.Clear();

BTW, in your second example I assume that you meant:

properties.AddRange(viewInterface.GetProperties());
 
A

Anderskj

Your problem, I believe, is that your interface can change. You should
think about designing the interface so that it won't have to.

Not exactly the answere I was looking for. I am developing a Windows
Workflow Foundation Custom Activity where one can set a textrepresentation
of an Interface. From this interface i by reflection must create a
collection of properties this custom activity can handle. I am aware of
"good" OO design but my requierement are a little ...different.

Anyway. I am sure it can be done. Other inputs? This is driving me crazy.

Regards
Anders
 
A

Anderskj

I suspect that the properties list already contains some items (48 to be
exact :). Try clearing it before you fill it: properties.Clear();

Did not see your other commenst. Sorry. Its not the problem. the 57
properties are actually comming from the type object. The 57 properties has
nothing to do with my interface.
BTW, in your second example I assume that you meant:
properties.AddRange(viewInterface.GetProperties());

YES. Of course! Thanks for pointing that out.

To clear things out. If i do like this:

// THIS WORKS (but is static) 9 interface properties
List<PropertyInfo> properties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetProperties());

// THIS DOES NOT WORK. 57 Type properties
// (properties from Type. Not what i want)
Type viewInterface = Type.GetType("app.IView");
properties.AddRange(viewInterface).GetProperties());

I want the the dynamic solution to work somehow.

Regards
Anders
 
G

Guest

It looks like you are somehow getting the type viewInterface again and
getting the properties over the Type objcet. I have this snippet:
List<PropertyInfo> properties = new List<PropertyInfo>();
properties.AddRange(typeof(IFoo).GetProperties());
List<PropertyInfo> properties2 = new List<PropertyInfo>();
Type fooInterface = Type.GetType("ConsoleApplication1.IFoo");
properties2.AddRange(fooInterface.GetProperties());

which results in properties and properties2 have the exact same number
properties. Be sure not to get the type of retrieved type again (in my
example, fooInterface).

Hope this helps
Deniz
 
A

Anderskj

It looks like you are somehow getting the type viewInterface again and
getting the properties over the Type objcet. I have this snippet:
List<PropertyInfo> properties = new List<PropertyInfo>();
properties.AddRange(typeof(IFoo).GetProperties());
List<PropertyInfo> properties2 = new List<PropertyInfo>();
Type fooInterface = Type.GetType("ConsoleApplication1.IFoo");
properties2.AddRange(fooInterface.GetProperties());
Hope this helps
Deniz

Helps??? Certianly did. You just saved my day.

You were absolutly right.. Changing:

properties2.AddRange(fooInterface.GetType().GetProperties());
to
properties2.AddRange(fooInterface.GetProperties());

of course did the job. Don't know why i wanted to get the Type again.
Anyway. Thanks alot.

Now I can finnally go to sleep (2 am....zzzZ).

Regards
Anders Jacobsen.
 
D

Dave Sexton

Hi Anders,

I see that you have solved your problem :)

I just wanted to add that you should post the exact code that is causing the
issue, or a short but complete (and accurate) example, in the future. (see
Jon Skeet's article about short but complete programs:
http://www.yoda.arachsys.com/csharp/complete.html).

The code you have posted here should work just fine (with the exception of a
slight parenthesis type-o) and so doesn't represent the problem you are
having.
 
A

Anderskj

I see that you have solved your problem :)
I just wanted to add that you should post the exact code that is causing
the issue, or a short but complete (and accurate) example, in the future.
(see Jon Skeet's article about short but complete programs:
http://www.yoda.arachsys.com/csharp/complete.html).

The code you have posted here should work just fine (with the exception of
a slight parenthesis type-o) and so doesn't represent the problem you are
having.

You are right. Should have posted more...throuhout code. But the problem was
actually representing my problem. There was a GetTyep(). to maby which Deniz
spotted. That fixed the problem.

Thanks for your help

Regards
Anders
 

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