using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Xunit.Abstractions;
namespace CustomerService.Tests;
public sealed class VariableTracker
{
private readonly ITestOutputHelper _testOutputHelper;
public VariableTracker(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}
[Fact]
public void ScanFile()
{
string filePath =
@"C:\Users\ozkan\projects\dotnet-examples\tc-guide-getting-started-with-testcontainers-for-dotnet\TestcontainersDemo\CustomerService\CustomerService.cs";
string code = File.ReadAllText(filePath);
SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
var root = (CompilationUnitSyntax)tree.GetRoot();
var variableCollector = new VariableCollector();
variableCollector.Visit(root);
_testOutputHelper.WriteLine("\nVariable Assignments:");
foreach (var assignment in variableCollector.Assignments)
{
if (assignment.Key.ToLower().Contains("command"))
_testOutputHelper.WriteLine($"Variable: {assignment.Key}, Assigned Value: {assignment.Value}");
}
}
private class VariableCollector : CSharpSyntaxWalker
{
public Dictionary<string, string> Assignments { get; } = new();
public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
{
var left = node.Left.ToString();
var right = node.Right.ToString();
Assignments[left] = right;
base.VisitAssignmentExpression(node);
}
}
}
Sunday, June 16, 2024
C# scan cs file and find variables values and names
Subscribe to:
Post Comments (Atom)
-
at the end of any product there will be a reporting interfaces for counts. let say you build a advertisement site which gives people to publ...
-
still in early steps and somehow I get the same http 500 error from docs.google.com. I dunno how to solve this.
-
geçen gün işden bi arkadaşla konuşuoz flash p2p yapıo dedim. olurmu lan öle şey windows firewall u var ne bilim modemden geçemez bi dünya an...
No comments:
Post a Comment