Tool to add code in my existing class
-
I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { consoleTextBox.ScrollToEnd(); }
Then I run a tool that will add log to all [LogThis] methods
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { log.Trace("starting ConsoleTextBox\_TextChanged"); consoleTextBox.ScrollToEnd(); }
Does somthin similar exists?
-
I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { consoleTextBox.ScrollToEnd(); }
Then I run a tool that will add log to all [LogThis] methods
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { log.Trace("starting ConsoleTextBox\_TextChanged"); consoleTextBox.ScrollToEnd(); }
Does somthin similar exists?
-
I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { consoleTextBox.ScrollToEnd(); }
Then I run a tool that will add log to all [LogThis] methods
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { log.Trace("starting ConsoleTextBox\_TextChanged"); consoleTextBox.ScrollToEnd(); }
Does somthin similar exists?
The term you're looking for is "Aspect-Oriented Programming[^]", or "AOP". There are a variety of tools which can do this, both free and commercial. There are also some articles here on CodeProject on the topic. For example: Aspect Oriented Programming in C# with RealProxy[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I would like to know if a toll exist to "inject" code in existing class. I'm thinking about a system working with attribute. These attributes are not used at compile or runtime but just to specify part of code I want to inject during coding. For example attribute [LogThis]
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { consoleTextBox.ScrollToEnd(); }
Then I run a tool that will add log to all [LogThis] methods
\[LogThis\] private void ConsoleTextBox\_TextChanged(object sender, TextChangedEventArgs e) { log.Trace("starting ConsoleTextBox\_TextChanged"); consoleTextBox.ScrollToEnd(); }
Does somthin similar exists?
While you have the information you need to explore AOP tools: very full-featured commercial, like PostSharp [^] ; open-source, like Fody [^]; and, others mentioned on this thread ... I wonder if there are other, simpler, techniques ... not based on Attributes and behind-the-scenes modification of Assemblies/IL code ... that could meet your needs ? These could include: use of #define compiler directives; run-time attaching/detaching EventHandlers. If you want to see examples of these methods, just ask. fyi: I use PostSharp, and it is an amazing tool-set.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot