Featured Post

A Evolução da Engenharia de Software

Introdução Nos últimos anos tem se observado uma crescente movimentação no mercado em torno do modelo de desenvolvimento denominado Fábrica de Software. Esse modelo tem uma grande característica que é o uso de técnicas utilizadas na engenharia industrial de produção em série, para a criação...

Read More

Obtendo as Properties pela ordem de declaração no C#

Posted by Luiz Picanço | Posted in .Net | Posted on 18-08-2009

Tags: , , ,

1,625

Em alguns momentos, pode ser necessário obter a lista de properties de um objeto. Um jeito simples de fazer isso é:

1
2
3
4
private IEnumerable<PropertyInfo> GetProperties(Type type)
{
    return type.GetProperties();
}

O problema é que, segundo a Microsoft, a ordem em que as properties são retornadas não é garantido:

The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which properties are returned, because that order varies.

Uma solução para resolver esse problema é ordenar a lista de properties pela property MetadataToken.

O código então, ficaria assim:

1
2
3
4
private IEnumerable<PropertyInfo> GetProperties(Type type)
{
    return type.GetProperties().OrderBy(p => p.MetadataToken);
}

Se a intenção for obter as properties na ordem inversa, o código ficaria assim:

1
2
3
4
private IEnumerable<PropertyInfo> GetPropertiesDescending(Type type)
{
    return type.GetProperties().OrderByDescending(p => p.MetadataToken);
}

O ideal é que o seu código não dependa da ordem dos membros para executar, mas em alguns casos, como geração automática de código, isso pode ser útil.

Comments (1,625)

Website worth visiting…

below you’ll find the link to some sites that we think you should visit…

News info…

I was reading the news and I saw this really cool topic…

Digg…

While checking out DIGG yesterday I found this…

iPhone 5 new’s and information…

[....]Thanks for taking the time to come and make sure we’re linking to your site! This message is only visible by you, the owner of[....]…

Tumblr article…

I saw someone talking about this on Tumblr and it linked to…

Dreary Day…

It was a dreary day here today, so I just took to piddeling around on the internet and found…

Digg…

While checking out DIGG yesterday I noticed this…

Looking around…

I like to look in various places on the internet, regularly I will go to Stumble Upon and follow thru…

Yahoo results…

While searching Yahoo I discovered this page in the results and I didn’t think it fit…

Just Browsing…

While I was browsing yesterday I noticed a excellent post about…

Blogs ou should be reading…

[...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……

Just Browsing…

While I was surfing today I saw a great article about…

Wikia…

Wika linked to this place…

Dreary Day…

It was a dreary day here yesterday, so I just took to messing around on the internet and realized…

is wartrol a scam…

[...]Wartrol Scam picture of female genital warts[...]…

Looking around…

I like to browse in various places on the web, often I will go to Digg and read and check stuff out…

Informative and precise…

Its hard to find informative and accurate info but here I noted…

Yahoo results…

While browsing Yahoo I found this page in the results and I didn’t think it fit…

Digg…

While checking out DIGG yesterday I found this…

News info…

I was reading the news and I saw this really cool information…

Yahoo results…

While browsing Yahoo I found this page in the results and I didn’t think it fit…

Digg…

While checking out DIGG today I found this…

Digg…

While checking out DIGG yesterday I found this…

Write a comment

You must be logged in to post a comment.