Skip to content

Decompile Code Can Not Compile In Visual Studio #444

Open
@VandyGan

Description

@VandyGan

dnSpyEx version

6.1.9-6.5.1

Describe the Bug

Source Code------>
public async Task<Tuple<bool, byte[]>> ReadBytesUntilMatchAsync(byte[] match, int timeout, CancellationToken cancellationToken)
{
return await Task.Run(() => ReadBytesUntilMatch(match, timeout, cancellationToken), cancellationToken);
}

    public async Task<Tuple<bool, byte[]>> ReadBytesUntilMatch(byte[] match, int timeout, CancellationToken cancellationToken)
    {
        bool isMatch = false;
        byte[] data;
        DateTime startTime = DateTime.Now;

        try
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                if (_serialPort.BytesToRead > 0)
                {
                    byte[] buffer = new byte[_serialPort.BytesToRead];
                    if (buffer.Length > 0)
                    {
                        await Task.Run(()=> _serialPort.Read(buffer, 0, buffer.Length)); 
                        lock (_bufferLock)
                        {
                            _byteBuffer.AddRange(buffer);
                            data = _byteBuffer.ToArray();
                            if (match.Length > 0 && ByteArrayContains(data, match))
                            {
                                isMatch = true;
                                break;
                            }
                        }
                    }
                    
                }

                if ((DateTime.Now - startTime).TotalMilliseconds > timeout)
                {
                    throw new TimeoutException();
                }

                Thread.Sleep(10); // Give time for data to be received
            }
        }
        catch (TimeoutException)
        {
            // Handle timeout if necessary
        }
        finally
        {
            lock (_bufferLock)
            {
                data = _byteBuffer.ToArray();
                _byteBuffer.Clear();
            }
        }

        return Tuple.Create(isMatch, data);

}

Decompile Code----->
public async Task<Tuple<bool, byte[]>> ReadBytesUntilMatch(byte[] match, int timeout, CancellationToken cancellationToken)
{
bool isMatch = false;
DateTime startTime = DateTime.Now;
byte[] data;
try
{
while (!cancellationToken.IsCancellationRequested)
{
bool flag = this._serialPort.BytesToRead > 0;
if (flag)
{
CustomSerialPort.<> c__DisplayClass18_0 CS$<> 8__locals1 = new CustomSerialPort.<> c__DisplayClass18_0();
CS$<> 8__locals1.<> 4__this = this;
CS$<> 8__locals1.buffer = new byte[this._serialPort.BytesToRead];
bool flag2 = CS$<> 8__locals1.buffer.Length != 0;
if (flag2)
{
await Task.Run(() => CS$<> 8__locals1.<> 4__this._serialPort.Read(CS$<> 8__locals1.buffer, 0, CS$<> 8__locals1.buffer.Length));
object obj = this._bufferLock;
lock (obj)
{
this._byteBuffer.AddRange(CS$<> 8__locals1.buffer);
data = this._byteBuffer.ToArray();
if (match.Length != 0 && this.ByteArrayContains(data, match))
{
isMatch = true;
break;
}
}
obj = null;
}
CS$<> 8__locals1 = null;
}
if ((DateTime.Now - startTime).TotalMilliseconds > (double)timeout)
{
throw new TimeoutException();
}
Thread.Sleep(10);
}
}
catch (TimeoutException)
{
}
finally
{
object obj2 = this._bufferLock;
bool flag4 = false;
try
{
Monitor.Enter(obj2, ref flag4);
data = this._byteBuffer.ToArray();
this._byteBuffer.Clear();
}
finally
{
int num;
if (num < 0 && flag4)
{
Monitor.Exit(obj2);
}
}
obj2 = null;
}
return Tuple.Create<bool, byte[]>(isMatch, data);
}
when i compile exe, the code compiled is not same with source code, and code compiled would be error in visual studio

How To Reproduce

when i compile exe, the code compiled is not same with source code, and code compiled would be error in visual studio

Expected Behavior

i expect that code compiled can be run in visual studio

Actual Behavior

when i compile exe, the code compiled is not same with source code, and code compiled would be error in visual studio

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions