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)
C# scan cs file and find variables values and names
using Microsoft . CodeAnalysis ; using Microsoft . CodeAnalysis . CSharp ; using Microsoft . CodeAnalysis . CSharp . Syntax ; using Xunit . ...
-
Yeni nesil hacker'lar: Edwin Pena | Olympos Security koptum okuyunca yazılımcılar ve pazarlamacılar bu kadar kısa ve özlü bir hikayeyle ...
-
https://leetcode.com/problems/odd-string-difference/ Beats 19.92% of users with Java class Solution { public String oddString ( S...
-
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...
No comments:
Post a Comment