public static void Using() { var code = @" namespace N { using System; using System.IO; internal class C { internal C(string fileName) { using (var disposable = File.OpenRead(fileName)) { } } } }"; var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindVariableDeclaration("disposable"); Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out ILocalSymbol symbol)); Assert.AreEqual(true, Disposable.Disposes(symbol, semanticModel, CancellationToken.None)); }
public static void WhenAddedToFormComponents() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { using System.IO; using System.Windows.Forms; public class Winform : Form { Winform() { var stream = File.OpenRead(string.Empty); // Since this is added to components, it is automatically disposed of with the form. this.components.Add(stream); } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindVariableDeclaration("stream = File.OpenRead(string.Empty)"); Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out ILocalSymbol symbol)); Assert.AreEqual(true, Disposable.Disposes(symbol, semanticModel, CancellationToken.None)); }
public static void IgnoreNewFormShow() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { using System.Windows.Forms; public class Winform : Form { public static void M() { var form = new Winform(); form.Show(); } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindVariableDeclaration("var form = new Winform()"); Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out ILocalSymbol symbol)); Assert.AreEqual(true, Disposable.Disposes(symbol, semanticModel, CancellationToken.None)); }