Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Finding Boxing calls using Roslyn
//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