Olá pessoal. Tudo certo?!
Vamos continuar explorando as novidades do .NET 4.5?! Na primeira parte, mostrei as classes para gerar arquivos zip. Hoje, mostro um importante acréscimo: Caller Information.
Acho que a melhor forma de explicar o conceito é com um código de exemplo. Observe:
Pegou a idéia?! Veja a informação do MSDN:
By using Caller Info attributes, you can obtain information about the caller to a method. You can obtain file path of the source code, the line number in the source code, and the member name of the caller. This information is helpful for tracing, debugging, and creating diagnostic tools.
To obtain this information, you use attributes that are applied to optional parameters, each of which has a default value.
Vejamos a descrição do MSDN:
You must specify an explicit default value for each optional parameter. You can’t apply Caller Info attributes to parameters that aren’t specified as optional.
The Caller Info attributes don’t make a parameter optional. Instead, they affect the default value that’s passed in when the argument is omitted.
Caller Info values are emitted as literals into the Intermediate Language (IL) at compile time. Unlike the results of the StackTrace property for exceptions, the results aren’t affected by obfuscation.
Ou seja, o compilador “completa o código”. Vejamos o IL para o código anterior:
Caller information é extremamente útil para construção de funções de depuração. Entretanto, o uso mais comum é a implementação de INotifyPropertyChanged.
Era isso.
Muito interessante, acho que agora rola uma demo usando com exemplo do INotifyPropertyChanged
Só eu que acho essa implementação da Microsoft *muito* zoada?
Realmente era essa a melhor forma de fazer isso?
Também achei muito estranho. Mas, não consigo pensar em algo melhor.
Como você faria?