Repository: Azure/azure-sdk-for-net
File: sdk/provisioning/Generator/src/Model/Specification.Analyze.cs (C#)

Code before:
        if (method is null) { return null; }

        MethodBody? body = method.GetMethodBody();
        if (body is null) { return null; }

        byte[] il = body.GetILAsByteArray()!;
        Module module = declaringType.Module;

        // Search for ldfld opcodes (0x7B) that reference RestOperations fields
        for (int i = 0; i < il.Length - 4; i++)
        {
            if (il[i] != 0x7B) { continue; } // OpCodes.Ldfld

            int token = il[i + 1] | (il[i + 2] << 8) | (il[i + 3] << 16) | (il[i + 4] << 24);
            try
            {
                FieldInfo? field = module.ResolveField(token);
                if (field is not null && field.FieldType.Name.EndsWith("RestOperations"))
                {
                    return field.FieldType;
                }
            }
            catch
            {
                // Skip invalid tokens
            }
            i += 4;
        }

        return null;
    }

    /// <summary>
    /// Extract the default api-version string from a RestOperations constructor
    /// by parsing its IL for string literals matching the api-version pattern.
    /// </summary>
    private static string? ExtractApiVersionFromRestOperations(Type restOperationsType)
    {
        ConstructorInfo? constructor = restOperationsType
            .GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
            .FirstOrDefault();
        if (constructor is null) { return null; }

        MethodBody? body = constructor.GetMethodBody();
        if (body is null) { return null; }

        byte[] il = body.GetILAsByteArray()!;
        Module module = restOperationsType.Module;

        // Search for ldstr opcodes (0x72) and find api-version string literals
        for (int i = 0; i < il.Length - 4; i++)

Review comment (suggestion):
we are processing ILs? this is so hacky and unmaintainable, we should not do this.
On the other hand, this will not work any way. Even if we find the correct RestOperations class, reflection will not be able to retrieve the value of api-version, because default api-version is set in this way in its ctor:
```
_apiVersion = apiVersion ?? "2025-05-01";
```

Code after:
    /// </summary>
    private static readonly Dictionary<string, string[]> s_fileContentCache = [];
    private static readonly HashSet<string> s_cachedDirectories = [];

    /// <summary>
    /// Cache all .cs files in the generation path directory.
    /// Called once per directory to ensure shared directories have all files cached
    /// before any spec cleans the directory.
    /// </summary>
    private void CacheGenerationDirectory()
    {
        string? generationPath = GetGenerationPath();
        if (generationPath is null || s_cachedDirectories.Contains(generationPath)) { return; }
        s_cachedDirectories.Add(generationPath);

        if (!Directory.Exists(generationPath)) { return; }
        foreach (string file in Directory.GetFiles(generationPath, "*.cs"))
        {
            if (!s_fileContentCache.ContainsKey(file))
            {
                s_fileContentCache[file] = File.ReadAllLines(file);
            }
        }
    }

    /// <summary>
    /// Read existing api-versions from the generated provisioning resource file.
    /// Returns separate lists for regular and hidden versions.
    /// Uses a static cache to handle shared output directories across specs.
    /// </summary>
    private (List<string> versions, List<string> hiddenVersions) ReadExistingResourceVersions(Resource resource)
    {
        List<string> versions = [];
        List<string> hiddenVersions = [];

        string? generationPath = GetGenerationPath();
        if (generationPath is null) { return (versions, hiddenVersions); }

        string filePath = Path.Combine(generationPath, $"{resource.Name}.cs");

        // Use cached file contents
        if (!s_fileContentCache.TryGetValue(filePath, out string[]? lines))
        {
            return (versions, hiddenVersions);
        }

        // Find the ResourceVersions class block
        bool inResourceVersions = false;
        int braceDepth = 0;
        bool nextVersionIsHidden = false;