// Program.cs – AttrInspector v3 // Requires SDK-style project (.NET 8+) and //…
- User
// Program.cs – AttrInspector v3 // Requires SDK-style project (.NET 8+) and //
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Loader; using System.Text;
internal static class AttrInspector { public static void Main(string[] args) { if (args.Length == 0) { Console.Error.WriteLine("Usage: AttrInspector [csv-output]"); return; }
var targetDll = Path.GetFullPath(args[0]); var csvPath = args.Length > 1 ? Path.GetFullPath(args[1]) : Path.Combine(AppContext.BaseDirectory, "foema_attributes.csv"); if (!File.Exists(targetDll)) { Console.Error.WriteLine($"DLL not found: {targetDll}"); return; } // ---------- assembly loading with dependency resolver ---------- var resolver = new AssemblyDependencyResolver(targetDll); var probingDir = Path.GetDirectoryName(targetDll)!; var alc = new AssemblyLoadContext("scan", isCollectible: true); alc.Resolving += (_, name) => { var path = resolver.ResolveAssemblyToPath(name) ?? Path.Combine(probingDir, $"{name.Name}.dll"); return File.Exists(path) ? alc.LoadFromAssemblyPath(path) : null; }; var asm = alc.LoadFromAssemblyPath(targetDll); Type[] types; try { types = asm.GetTypes(); } catch (ReflectionTypeLoadException ex) { types = ex.Types.Where(t => t is not null).ToArray()!; Console.Error.WriteLine($"[WARN] {ex.LoaderExceptions!.Length} referenced types could not be loaded and were skipped."); } var rows = new List<(string Source, string Entity, string Attr)>(); // ---- 1) literals inside *.AttrNames ---- foreach (var ent in types) { var attrNames = ent.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic) .FirstOrDefault(nt => nt.Name == "AttrNames"); if (attrNames == null) continue; foreach (var f in attrNames.GetFields(BindingFlags.Public | BindingFlags.Static)) { if (!(f.IsLiteral && f.FieldType == typeof(string))) continue; var lit = (string)f.GetRawConstantValue()!; if (!lit.StartsWith("foema_", StringComparison.OrdinalIgnoreCase)) continue; rows.Add((Source: ent.Name, Entity: ent.Name, Attr: lit)); } } // ---- 2) static-readonly arrays called *Attributes ---- foreach (var t in types) { foreach (var f in t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { if (!(f.FieldType == typeof(string[]) && f.Name.EndsWith("Attributes", StringComparison.OrdinalIgnoreCase))) continue; if (f.GetValue(null) is not string[] values) continue; // entity derived from first segment before '.' var entity = t.Name.Replace("Repository", string.Empty, StringComparison.OrdinalIgnoreCase); foreach (var val in values.Where(v => v.StartsWith("foema_", StringComparison.OrdinalIgnoreCase))) rows.Add((Source: t.Name, Entity: entity, Attr: val)); } } // ---- 3) de-duplicate ---- var distinct = rows.Distinct(); // ---- 4) write CSV ---- Directory.CreateDirectory(Path.GetDirectoryName(csvPath)!); using var sw = new StreamWriter(csvPath, false, Encoding.UTF8); sw.WriteLine("source,entity,attribute"); foreach (var r in distinct.OrderBy(r => r.Source).ThenBy(r => r.Attr)) sw.WriteLine($"{Csv(r.Source)},{Csv(r.Entity)},{Csv(r.Attr)}"); Console.WriteLine($"✓ Wrote {distinct.Count()} rows to {csvPath}"); alc.Unload(); } private static string Csv(string s) { // naive escape: enclose in quotes if needed and double any embedded quotes return s.Contains(',') || s.Contains('"') ? $"\"{s.Replace("\"", "\"\"")}\"" : s; }} explain on a non technical level what this code does, an how reliable the results are:
Assistant
This code is a tool designed to scan a .NET DLL file (a compiled C# library) and extract specific attribute names that start with the prefix
"foema_". These attributes are likely related to some internal system or framework (possibly named "Foema") within the codebase. The results are saved in a CSV file for easy analysis.
What the Code Does (In Simple Terms)
-
Input:
- You give it the path to a
.dllfile (likeMyApp.dll) and optionally a CSV output file. - If no CSV path is provided, it defaults to a file named
foema_attributes.csvin the same folder as the tool.
- You give it the path to a
-
Loading the DLL:
- The tool tries to load the DLL and its dependencies (other
.dllfiles it depends on) using a special "loader" to avoid conflicts with other parts of the system.
- The tool tries to load the DLL and its dependencies (other
-
Scanning for "foema_" Attributes:
- It searches through the types (classes, structs, etc.) in the DLL for two specific patterns:
- Pattern 1: Nested classes called
AttrNameswith static string literals (likepublic const string foema_SomeAttribute = "value";). - Pattern 2: Static arrays of strings (like
public static readonly string[] FoemaAttributes = { "foema_SomeAttribute", ... };).
- Pattern 1: Nested classes called
- Any string starting with
foema_is considered a candidate attribute.
- It searches through the types (classes, structs, etc.) in the DLL for two specific patterns:
-
Organizing Results:
- It collects the results in a list, deduplicates them, and writes them to a CSV file with three columns:
source: The name of the class/struct where the attribute was found.entity: A cleaned-up version of the source name (e.g., removing "Repository" fromCustomerRepositoryto getCustomer).attribute: The actualfoema_attribute name (likefoema_SomeAttribute).
- It collects the results in a list, deduplicates them, and writes them to a CSV file with three columns:
-
Output:
- The CSV file is saved, and the tool reports how many unique attributes were found.
How Reliable Are the Results?
The reliability depends on how consistently the target DLL follows the patterns the code expects:
Pros (Likely Reliable if Patterns Match):
- If the DLL uses
AttrNamesnested classes or*Attributesarrays withfoema_prefixes, this tool will find them. - It handles common edge cases (e.g., missing dependencies, malformed types) gracefully by logging warnings.
Cons (Potential Limitations):
- False Negatives:
- If the code uses different naming conventions (e.g.,
MyAttrsinstead of*Attributes), the tool will miss those attributes. - If the
foema_attributes are not stored in the expected patterns (e.g., hardcoded in methods or generated dynamically), they won’t be found.
- If the code uses different naming conventions (e.g.,
- False Positives:
- If a
foema_string appears in unrelated contexts (e.g., a comment or a regular field), it might be incorrectly included.
- If a
- Dependency Issues:
- If the DLL has unresolved dependencies (e.g., missing
.dllfiles), the tool might fail to load some types, leading to incomplete results.
- If the DLL has unresolved dependencies (e.g., missing
- Assumptions:
- The code assumes that "entities" (like
Customer) are derived by stripping "Repository" from the source class name. This might not work for all naming conventions.
- The code assumes that "entities" (like
When to Use This Tool
- If you're analyzing a .NET codebase and want to catalog all
foema_attributes used in a specific DLL. - If the code follows consistent naming patterns (like
AttrNamesand*Attributesarrays).
When Not to Use This Tool
- If the DLL uses unconventional naming or stores attributes in non-static fields/methods.
- If the DLL has complex dependencies or is not built for .NET 8+ (the code requires SDK-style projects and specific NuGet packages).
Summary
This tool is a specialized scanner for
.NET DLLsthat looks forfoema_attributes using specific patterns. It’s reliable if the target code follows the expected structure, but may miss attributes if the code deviates from those patterns. It’s useful for quick analysis but not a universal solution for all .NET codebases.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.