473,406 Members | 2,377 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,406 software developers and data experts.

Get properties on interface (runtime/reflection)

Hi!

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

If i do like this:

List<PropertyInfoproperties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetPropertie s());

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).GetPropertie s());

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


Dec 9 '06 #1
7 6042
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<PropertyInfoproperties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetPropertie s());

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).GetPropertie s());

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()) ;
--
Dave Sexton
Dec 9 '06 #2
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
Dec 9 '06 #3
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<PropertyInfoproperties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetPropertie s());

// 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
Dec 9 '06 #4
It looks like you are somehow getting the type viewInterface again and
getting the properties over the Type objcet. I have this snippet:
List<PropertyInfoproperties = new List<PropertyInfo>();
properties.AddRange(typeof(IFoo).GetProperties());
List<PropertyInfoproperties2 = 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

"Anderskj" wrote:
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<PropertyInfoproperties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetPropertie s());

// 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
Dec 10 '06 #5
It looks like you are somehow getting the type viewInterface again and
getting the properties over the Type objcet. I have this snippet:
List<PropertyInfoproperties = new List<PropertyInfo>();
properties.AddRange(typeof(IFoo).GetProperties());
List<PropertyInfoproperties2 = 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().GetPro perties());
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.
Dec 10 '06 #6
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.

--
Dave Sexton

"Anderskj" <anderswrote in message
news:OJ****************@TK2MSFTNGP02.phx.gbl...
>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<PropertyInfoproperties = new List<PropertyInfo>();
properties.AddRange(typeof(app.IView).GetPropertie s());

// 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

Dec 10 '06 #7
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
Dec 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: trenchmouth | last post by:
Does anybody know how I can loop through all the properties in a class I have created? Can I use Me to refer to the properties? I'm using VB6. Many thanks.
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Microsoft | last post by:
Hello there, I have following situation , how can I solve this puzzle in C#. I have interface called IVendor with some custom attributes applied to it interface IVendor {
3
by: Martin Montgomery | last post by:
I have, for example, a property called myProperty. I would like, when using a property grid to display the property name as "My Property". Is this possible. Is there an attribute etc Thank ...
3
by: Phill. W | last post by:
(VB.Net 2003) I have a UserControl that exposes a number of public properties. When using this control on a.n.other Form, how can(?) I prevent the Forms Designer from adding code into...
4
by: ABC | last post by:
I want to check the form's controls have or not the specific properties or events. How to determine or gather the properties list under the run-time environment?
3
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
10
by: Derek Hart | last post by:
I am going in circles trying to loop through properties on 3rd party controls. For example, I have a textbox that has its maximum length located at MyTextBox.Properties.MaxLength - instead of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.