Find all places a method is used in my views - c#

Sprinkled throughout my views I make calls to a method that translates text into a user's preferred language. For example:
#Translate("Translate this for me")
Now I want to find every call to this method so that I can compile a list of all the text that needs to be translated in my MVC project.
I've used reflection before to access all of my controllers and action methods, but how can I access the views and the method calls therein? Or is there another way to do this without using reflection? Perhaps the razor engine can be called upon to help with this somehow?
EDIT:
The "Translate" method is in a BaseWebViewPage that all my views inherent from
UPDATE:
Thanks for your comments, I hadn't thought of using 'find' in Visual Studio for this - it's a really practical answer, much appreciated. Although, while we're here, I wonder if there is a programmatic way to do this?

Related

Extending Intellisense on specific language constructs (namely Attribute constructors)

I've written a custom attribute for decorating methods. The attributes help add non-critical but very helpful contextual data to the method. Currently, this helps with debugging, but eventually it will help with static analysis.
Because the Master of the data specified in the attributes is on a server and queriable, I figured I'd write a Visual Studio Intellisense extention to help my developers. The extension would kick in when typing in parameters in my Attribute's constructor. The completion suggestions would essentially be query results.
I've read and implemented a Statement Completion example on MSDN with relative success.
The example is for Completion on PlainText. I've changed it to work on Code instead. I'm one step closer. The problem I'm having, however, is that I can't seem to find how to make it aware of its context. The Completion kicks an whenever I type any code. I'd like it to be a bit more discriminating than this.
Question: Is it possible to make a custom Visual Studio Statement Completion extension aware of the language constructs around it?
I'd like to be able to only trigger Statement Completion when I'm typing into an Attribute's constructor. And, if possible, preferably only on Attributes derived from a given base class.
Is this kind of thing possible?
No. This is why we're building Roslyn, but until then you might be able to use a third-party parser, or your own hacked up parser or heuristics to make it work.

using attribute to read Method Parameters

I want to log the entry of methods. In entry log I would have inputs\parameters received by the method. This has to be done for thousands of methods.
I thought of doing this logging of input parameters using C# ATTRIBUTES, since they fired before method call. (Something similar to ActionFilters in MVC)
Is that possible to read method parameters through attributes?
The concept you are looking for is called aspect oriented programming (AOP). It is a technique that allows you to "weave" in blocks of boilerplate code across your application code. Logging is a perfect example for that. You can either go the hard way and implement logging before and after each method call manually (which is on the one hand not feasible in large projects and on the other hand error prone).
Or you can use an AOP Framework that allows you to define these cross cutting functions in one place and apply it declaratively to your application code. There are several approaches to achieve this; one is to create IL after the build of the application logic and therefore integrating the aspects at compile time. A well known example for this is PostSharp. There also is a free edition that is good for the start.
BTW: PostSharp heavily relies on attributes, so you're on the right track.
Another option is to integrate the aspects at run time (keyword is interception). Most IoC Frameworks offer this. This approach is easy to use but has some downsides IMHO (weaker runtime Performance, only virtual methods can be intercepted).
Attributes are not 'fired before method call', the code that invokes a method that is decorated with an Attribute may (or may not) do something based on the presence of the Attribute.
The Attribute doesn't know the member it is applied on, nor can access it in any (straight forward) way.

How do I find what methods are called from a C# class method - NOT at run time

I have a C# class that has far too much code in, and I want to refactor it. What I would like to do is start with all the public methods, and build a tree for each one, showing which other methods in the class are called from it, and then which are called from the child one, and so on.
This will enable me to see which private methods belong solely to one public method, which are shared and so on.
Note that I DON'T want to do this at run time, I want to be able to look at a class, either directly at the .cs file, or using reflection on the compiled DLL.
I know I can use reflection on the compiled DLL to get the methods, but I can't find any way of finding out which methods are called by other methods in the class.
Anyone any ideas? Again, this is NOT a run time issue, it's purely to build a reusable utility to help refactor an oversized class. There are quite a few in the solution I'm working on, so the code woudl be used over and over again.
Visual Studio 2010 has action "View Call Hierarchy" where you can see all places in your solution where your code is invoked.
From my experience this static analysis may be somewhat lacking, for example method could be called dynamically using Reflection, through Data Binding, through Dependency Injection Container, etc.
Also, that may be bit off topic, and not applicable in your case, but I find a good way to find dead code for component is to have a suite of integration tests. Then you can run code coverage, and visually see what paths of code are never executed.

Is there a way to make Anonymous classes with intellisense or something approaching?

In my projects, I frequently use viewmodels (simple classes used just for transferring data to view) like ProductAdditionnalInformationForCustomFees classes who are used just to render data and used in few times in the project.
But, actually I have a lot of these and I think it's not the best way to handle data transfer between controller and view.
I could use anonymous classes but i would lost the properties intellisense who greatly improve development time and bug hunting.
Is there a way to make something like anonymous classes but with intellisense?
Anonymous classes are marked Internal. This makes it difficult to share between your view and controller. There are workarounds but stick with the strongly typed classes.
One possible and interesting workaround but you won't get intellisense:
Dynamic Anonymous type in Razor causes RuntimeBinderException

Refactoring duplicate code into methods

I am using Resharper for refactoring. What is this type of feature called when you have the same code in around 5 different events being called but you want to replace them with a method call? And place that code in that method.
All done automatically.
Thanks
I've been working on a Resharper plugin that does what you are asking. That is, it scans your code, searching for sections that can be replaced by an existing method call. A section can be a whole method or just a part of a method. When it finds one, the lightbulb pops up and offers to replace said section with a call to the existing method.
(source: landofjosh.com)
I call it AgentRalph. At this point it's not ready for production use, but I've been making a lot of progress and hope to make a release soon.
Extract Method.
See our C# CloneDR. While it doesn't replace redundant code with function calls, it does tell you where they are across very large system, and forms the essential abstraction (procedure body and parameters). The web link has example clone analyses for the C# equivalent of Hibernate (NHibernate).

Categories

Resources