For Each Property In a Class

2 views
Skip to first unread message

Taner Eser

unread,
Nov 3, 2009, 2:50:28 PM11/3/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi all.

Consider this;

I have a class with bunch of properties and i would like to do exactly
same action for each property in my class.

Example;

foreach(property myProperty in MyClassName.Properties)
{
//Do the job!
}

Any suggestions?

Cerebrus

unread,
Nov 4, 2009, 1:41:44 AM11/4/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I think that the Type.GetProperties function provides exactly what you
need. (Why you need it is another matter, altogether).

Paulo Roberto Pellucci

unread,
Nov 4, 2009, 6:04:27 AM11/4/09
to dotnetde...@googlegroups.com
Start a search about reflection, that is what you'll need.
--
Atenciosamente,
Paulo Roberto S. Pellucci

Felipe Teixeira

unread,
Nov 4, 2009, 10:58:58 AM11/4/09
to dotnetde...@googlegroups.com
Generics + Reflection + Abstract Class = FUN :D

Be aware that when using Reflection you are adding some delay to the process.

Felipe Teixeira
Microsoft Certified Technology Specialist - MCTS - Microsoft .Net Framework 2.0
Application Development Foundation MS
Web -based Client Development
Mobile: +55 (11) 8021-6457
http://iguessimnotcrazy.wordpress.com/


2009/11/4 Paulo Roberto Pellucci <paulo.p...@gmail.com>

Jamie Fraser

unread,
Nov 5, 2009, 4:43:33 AM11/5/09
to dotnetde...@googlegroups.com
And in 99% of cases the "overhead" is negligible. Don't be scared of reflection.

Felipe Teixeira

unread,
Nov 5, 2009, 6:43:39 AM11/5/09
to dotnetde...@googlegroups.com
I woldn't say 99%... not even 0,000001%.

Overhead is never negligible.

Other than that, reflection is not a monster anyway.. Use it wisely and everything should be just fine.

Felipe Teixeira
Microsoft Certified Technology Specialist - MCTS - Microsoft .Net Framework 2.0
Application Development Foundation MS
Web -based Client Development
Mobile: +55 (11) 8021-6457
txdo...@googlewave.com
http://iguessimnotcrazy.wordpress.com/



2009/11/5 Jamie Fraser <jamie....@gmail.com>

Felipe Teixeira

unread,
Nov 5, 2009, 1:03:07 PM11/5/09
to dotnetde...@googlegroups.com
BTW: you might also want to take a look into static reflection...

http://ayende.com/Blog/2005/10/29/StaticReflection.aspx


Felipe Teixeira
Microsoft Certified Technology Specialist - MCTS - Microsoft .Net Framework 2.0
Application Development Foundation MS
Web -based Client Development
Mobile: +55 (11) 8021-6457
txdo...@googlewave.com
http://iguessimnotcrazy.wordpress.com/


2009/11/5 Felipe Teixeira <txdo...@gmail.com>
Reply all
Reply to author
Forward
0 new messages