tags:

views:

48

answers:

2

Is there any way to get the instance class inside the attribute codein C#?

public class MyAttirbute : Attribute {
    public someMethod(){
       this.InstanceClass????
    }
}
+2  A: 

No, unfortunately not.

Attributes are per type and per member (ie. per "something" they're attached to), not per instance.

You could even inspect the attributes and call those methods on it without there ever being any instances of the class it has been applied to.

What are you trying to do that requires this?

Lasse V. Karlsen
I'm tryng to implement a custom validation attribute that inherits from System.ComponentModel.DataAnnotations.ValidationAttribute, but I need information about other property in the instace to validade, this is the way I'm trying to get the object instace associated.
Murilo Lima
@Murilo Lima: Check out http://pastebin.com/cvRkV2Ux. It has a few problems, but is ok in general.
Yuriy Faktorovich
@Murilo: don't put code in your attributes! Have your validator look at different attributes of the class, don't have your attribute do the validation! If you need to declare that validation must refer to another property in some way, have a string value that contains the name of the property and use reflection to obtain the value.
Randolpho
@Randolpho: The attributes in the DataAnnotation namespace know how to validate themselves.
Yuriy Faktorovich
@Yuriy: That's not entirely accurate. They have `Validate` methods that are passed an instance for validation by the validation engine, yes, but they're not able to validate the instances to which they're applied all by themselves. I consider it a non-solid architecture, frankly. I think the attribute should be for marking and providing metadata, with a separate validator class doing the work of actual validation.
Randolpho
@Randolpho: Are you familiar with PostSharp?
Yuriy Faktorovich
@Yuriy: Yes, although I don't use it. Coopting attributes to provide AOP with IL rewriting is a good hack, to be sure, but it's not pure C#/.NET, and there's nothing to indicate that the original asker is trying to do AOP in the first place.
Randolpho
@Randolpho: I was asking more out of curiosity as it goes against your tenant of *don't put code in your attributes!*. I think the code I pasted is what the asker was looking to implement. Possibly with the addition of AOP, but that is very questionable.
Yuriy Faktorovich
@Yuriy: Well... as I said, it's a great hack, but keep in mind that they coopt attributes for their own uses. My problem with "code in an attribute" stems more from SOLID principles than anything else. An attribute is for attaching metadata to a method, field, class, or parameter. Adding logic that doesn't deal strictly with the metadata itself breaks Single Responsibility. *Edit re: your edit:* The code you pasted is static; it doesn't operate on any instance. It could totally live somewhere else and still do exactly what the original poster wanted to do.
Randolpho
@Randolpho: True, that method could live elsewhere, and it does likely break SRP. I hope if the OP finds some use in what I posted, they will take that method out of there.
Yuriy Faktorovich
I've used an attribute that targets a class and is initialized with the properties names, then I could get the values through reflection.
Murilo Lima
+1  A: 

If I understand you correctly, you want to get the Type ("class") where an attribute is applied on.

This information is not available unfortunately, but you can only obtain it the other way around using reflection by visiting all types in an assembly and getting their custom attributes.

Lucero
No, I need the exact instance of the class associeted.
Murilo Lima
It's important to understand that attributes are only applied to metadata (assembly, type, members), and not instantiated per instance. There is no way to get the instance(s) belonging to a certain attribute without keeping track of the existing instances yourself (for instance via a base class which keeps a collection of weak references to all instances ever created).
Lucero