How can I read custom Attributes without using reflection?

Asked

Viewed 45 times

3

I have the following code snippet:

[Campo(Chave=true)]
public System.Guid EscalaId { get; set; }

I know I can read these tributes using class MemberInfo, as it says example in the documentation:

System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);

But there are alternatives without the use of reflection?

  • 1

    What is the purpose of this?

  • Essentially there is no way. There are even absurd forms.

  • Do reflection without reflection. To me it sounds kind of strange.

  • It is a part of a system, which relies on class attributes to mount SQL commands. I read that reflection can be slow compared to other tools, and as this feature of assembling queries is widely used, I decided to search to see if there was any alternative, as I found only examples with reflection, I decided to make a last attempt here

1 answer

3


How can I read custom Attributes without using reflection?

There’s no way.

As the problem is the matter of speed, the speed preblema in Reflection is in the reading and definition of property values, not in reading decoration attributes.

In addition, there are libraries to solve this. The most famous of them is the Fastmember, that is part of the Dapper, who does just that: reads several things by Reflection, but the value setting uses dynamically generated static code at runtime, by Reflection.Emit.

  • Thank you very much. I’m sorry about the quick change of subject, but Dapper is used in Stackoverflow itself, right? I remember reading that

  • Yes, it is the micro-ORM query and persistence database here of the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.