Created
June 20, 2016 08:45
-
-
Save sudipto80/43efdecb878cac17b340cda2c281c3b3 to your computer and use it in GitHub Desktop.
Finding Boxing calls using Roslyn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Avoid Boxing | |
var code = @"public void fun(){int x = 32; | |
object o = x;}"; | |
var tree = CSharpSyntaxTree.ParseText(code); | |
//x - Int | |
//o -> object | |
// | |
var objects = tree.GetRoot() | |
.DescendantNodes() | |
.OfType<VariableDeclarationSyntax>() | |
.SelectMany(aes => | |
aes.Variables.Select(v => | |
new { Type = aes.GetFirstToken().ValueText, | |
Name = v.Identifier.ValueText, | |
Value = aes.GetLastToken().ValueText}) | |
); | |
var defaulters = objects.Where(aes => aes.Type == "object" && | |
objects.FirstOrDefault(d => d.Name == aes.Value | |
&& d.Type!="object") != null ) | |
.Dump(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment