2

I have an existing C# class that I cannot modify, but only use it. By example:

public class Test {}

I would like to add dynamically an attribute (MyAttribute) to this class Test, like:

[MyAttribute(TestMode)]
public class Test {}

Is it possible in C# and how?

Note: of course, I can derive from this Test class and add declaratively the attribute, but it means to create a new class, and I'd like to avoid it.

6
  • 1
    What do you mean when you say dynamically? Do you want to add the attribute at runtime? Commented Dec 1, 2011 at 10:24
  • Check this thread: stackoverflow.com/questions/129285/… Commented Dec 1, 2011 at 10:27
  • I second Daniel here. Please clarify what you are trying to accomplish. Commented Dec 1, 2011 at 10:27
  • @Daniel: Yes, I want to add the attribute at runtime Commented Dec 1, 2011 at 10:38
  • Maybe you misunderstood what attributes are. Attributes are information about a Class and therefore it's absolutely not related with dynamically add information to an Object. What do you want do do? I'm sure the solution is something else. Commented Dec 1, 2011 at 10:40

1 Answer 1

6

Attributes are compiled into metadata and cannot be added at runtime.

http://msdn.microsoft.com/en-en/library/z0w1kczw(v=vs.80).aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.