Repository: dotnet/runtime
File: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/SOSDacImpl.cs (C#)

Code before:
            if (typeSystemContract.IsFreeObjectMethodTable(typeHandle))
            {
                OutputBufferHelpers.CopyStringToBuffer(className, count, pNeeded, "Free");
            }
            else
            {
                TargetPointer modulePointer = typeSystemContract.GetModule(typeHandle);
                Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(modulePointer);
                if (!loader.TryGetLoadedImageContents(moduleHandle, out _, out _, out _))
                {
                    OutputBufferHelpers.CopyStringToBuffer(className, count, pNeeded, "<Unloaded Type>");
                }
                else
                {
                    System.Text.StringBuilder classNameBuilder = new();
                    try
                    {
                        TypeNameBuilder.AppendType(_target, classNameBuilder, typeHandle, TypeNameFormat.FormatNamespace | TypeNameFormat.FormatFullInst);
                    }
                    catch
                    {
                        string? fallbackName = _target.Contracts.DacStreams.StringFromEEAddress(mt);
                        if (fallbackName != null)
                        {
                            classNameBuilder.Clear();
                            classNameBuilder.Append(fallbackName);
                        }
                    }
                    OutputBufferHelpers.CopyStringToBuffer(className, count, pNeeded, classNameBuilder.ToString());
                }
            }
        }
        catch (System.Exception ex)
        {
            hr = ex.HResult;
        }

#if DEBUG
        if (_legacyImpl is not null)
        {
            char[] classNameLocal = new char[count];
            uint neededLocal;
            int hrLocal;
            fixed (char* ptr = classNameLocal)
            {
                hrLocal = _legacyImpl.GetObjectClassName(obj, count, ptr, &neededLocal);
            }
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
            if (hr == HResults.S_OK)
            {
                Debug.Assert(pNeeded == null || *pNeeded == neededLocal);

Review comment (suggestion):
nit: 
```suggestion
            Contracts.IObject object = _target.Contracts.Object;
```

Code after:
#if DEBUG
        if (_legacyImpl is not null)
        {
            ClrDataAddress exceptionObjectLocal;
            ClrDataAddress nextNestedExceptionLocal;
            int hrLocal = _legacyImpl.GetNestedExceptionData(exception, &exceptionObjectLocal, &nextNestedExceptionLocal);
            Debug.Assert(hrLocal == hr, $"cDAC: {hr:x}, DAC: {hrLocal:x}");
            if (hr == HResults.S_OK)
            {
                Debug.Assert(*exceptionObject == exceptionObjectLocal);
                Debug.Assert(*nextNestedException == nextNestedExceptionLocal);
            }
        }
#endif
        return hr;
    }

    int ISOSDacInterface.GetObjectClassName(ClrDataAddress obj, uint count, char* className, uint* pNeeded)
    {
        int hr = HResults.S_OK;
        try
        {
            if (obj == 0)
                throw new ArgumentException();

            Contracts.IObject objectContract = _target.Contracts.Object;
            Contracts.IRuntimeTypeSystem rts = _target.Contracts.RuntimeTypeSystem;
            Contracts.ILoader loader = _target.Contracts.Loader;

            TargetPointer mt = objectContract.GetMethodTableAddress(obj.ToTargetPointer(_target));
            Contracts.TypeHandle typeHandle = rts.GetTypeHandle(mt);

            TargetPointer modulePointer = rts.GetModule(typeHandle);
            if (modulePointer == TargetPointer.Null)
            {
                OutputBufferHelpers.CopyStringToBuffer(className, count, pNeeded, "<Unloaded Type>");
            }
            else
            {
                Contracts.ModuleHandle moduleHandle = loader.GetModuleHandleFromModulePtr(modulePointer);
                if (!loader.TryGetLoadedImageContents(moduleHandle, out _, out _, out _))
                {
                    OutputBufferHelpers.CopyStringToBuffer(className, count, pNeeded, "<Unloaded Type>");
                }
                else
                {
                    StringBuilder classNameBuilder = new();
                    try
                    {
                        TypeNameBuilder.AppendType(_target, classNameBuilder, typeHandle, TypeNameFormat.FormatNamespace | TypeNameFormat.FormatFullInst);
                    }