How to avoid or detect implicit delegate output in C #?

I am writing a game using C # and found a number of cases where a function accepts a delegate, and I accidentally passed the name of the function instead of creating and caching the delegate to use as a parameter. This leads to the creation of a delegate object for each call to these functions, which then immediately becomes garbage when the function returns.

I would like to find all the places where I made this error, and I would prefer not to read every line of every file looking for them (there are many years of code). I saw how VB has a “strict option” that disables implicit building of objects, which I think will work for me if C # has this function, but I don't think so. I also looked at the compiler warning options, and none of them seem to help here.

Is there a reasonably convenient way to identify these objects created by implicit delegate delegation so that I can find out where I need to create / cache callbacks to avoid garbage?

+6
source share
1 answer

In short, your question is: "How can I find all the transformations of method groups?"

We are currently working on a project codenamed Roslyn, which will allow you to use the same semantic analysis mechanism that the C # compiler and IDE use. He will open the syntactic model of the language, and then provide the semantic analysis API with which you can ask questions of the semantic analyzer.

With Roslyn, you can compile all your code into syntax trees and then search for these syntax trees for each expression. There will be an API that allows you to determine if an expression has been converted to anything, and if so, how the transformation analyzer has classified the transformation.

We are currently at the “community technology preview” stage; we have a preliminary implementation, but it is not yet fully available. I don’t remember whether the method group conversion analyzer was implemented in the CTP release or not.

Try it, and if you have any feedback about this, we will be happy to hear your thoughts on the Roslyn forum.

Details here:

http://msdn.microsoft.com/en-us/roslyn

+10
source

Source: https://habr.com/ru/post/914158/


All Articles