0

Lets say in .NET/C# I have a bunch of static classes in a namespace. I don't want to manually change the code but I'd like to make them all call a specific function when entering a function. It would be nice if I could pass in the class/function name but not required. How might I do this?

2
  • Is this for analytical purposes or a permanent thing? Commented Feb 3, 2013 at 4:05
  • @500-InternalServerError: Not my call but probably a permanent thing if functions/classes can be filtered out Commented Feb 3, 2013 at 16:41

2 Answers 2

2

You are looking for something called Aspect Oriented Programming or AOP.

Try the PostSharp library

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

2 Comments

But it looks like I need to manually put [attr] on all of them:/
That's all i've got as far as libraries. If you don't like that option then you can always write something to modify the code or the IL yourself.
1

If you want to do this without modifying the source code, you will be looking at modifying the IL directly. In fact, "PostSharp" itself does rewrite your assembly IL, as do many aspect-oriented libraries.

You could theoretically rewrite your the IL yourself, (and a project such as Cecil might help) without modifying the source - however, I doubt it would be worth the effort.

Comments

Your Answer

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