Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created June 20, 2016 08:45
Show Gist options
  • Select an option

  • Save sudipto80/43efdecb878cac17b340cda2c281c3b3 to your computer and use it in GitHub Desktop.

Select an option

Save sudipto80/43efdecb878cac17b340cda2c281c3b3 to your computer and use it in GitHub Desktop.

Revisions

  1. sudipto80 created this gist Jun 20, 2016.
    28 changes: 28 additions & 0 deletions avoidBoxing.linq
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    //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();