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

Get properties on interface (runtime/reflection)

P: n/a
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
Share this Question
Share on Google+
7 Replies


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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.