The best way to access attributes

I am working on a framework that uses some attribute markup. This will be used in the MVC project and will happen approximately every time I look at a specific record in the view (e.g. / Details / 5)

I was wondering if there is a better / more efficient way to do this or a good example of best practices.

Anyway, I have several attributes, for example:

[Foo("someValueHere")] String Name {get;set;} [Bar("SomeOtherValue"] String Address {get;set;} 

What is the most efficient way / best practice for finding these attributes / Act on their values?

I am currently doing something like this:

 [System.AttributeUsage(AttributeTargets.Property)] class FooAttribute : Attribute { public string Target { get; set; } public FooAttribute(string target) { Target = target; } } 

And in my method, where I work on these attributes (simplified example!):

 public static void DoSomething(object source) { //is it faster if I make this a generic function and get the tpe from T? Type sourceType = source.GetType(); //get all of the properties marked up with a foo attribute var fooProperties = sourceType .GetProperties() .Where(p => p.GetCustomAttributes(typeof(FooAttribute), true) .Any()) .ToList(); //go through each fooproperty and try to get the value set foreach (var prop in fooProperties) { object value = prop.GetValue(source, null); // do something with the value prop.SetValue(source, my-modified-value, null); } } 
+7
source share
2 answers

Attribute.GetCustomAttribute and PropertyInfo / MemberInfo.GetCustomAttribute are the recommended way to access attribute objects.

Although, I would usually not list all properties with attributes; you usually want to work with a specific attribute, so you just call GetCustomAttribute directly. If you are looking for attributes for any of your properties, listing those properties that are looking for attributes based on GetCustomAttribute (), like "Doing this, this is the best way to do this."

+2
source

Actually, there is not much choice when you are dealing with attributes - your code is in order and more reasonable, as in other cases, this does not apply to your main activity. The only thing to do is to refuse to call ToList as absolutely unnecessary.


Side notes: a performance question should look something like

"I measured my code, and part XXX seems to take too much time (YYY). The goal of the time for this part of the code is ZZZ. Is my way of making XXX reasonable / where can I improve it?",

Please note that in your case you are missing the YYY and ZZZ parts, so you cannot say that this is a slow case for your case or not. And you can start the measurements using DB / other IO operations, as this would rather speed up your general code.

Once you understand that this attribute-related code is a major performance issue, you can consider some sort of caching of results or even generating some kind of code (either by caching lambdas that would set the necessary values ​​or even completely bloat the generation IL).

+1
source

All Articles