VB -> C#: Static variables are supposed to be lazy initialized inside method/property

Aperta
#868 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
csharp, visualbasic
Ambito
tooling

Direzione di ricerca

Riproduci la conversione VB-to-C# dell’issue nel sito web CodeConverter, quindi segui il percorso di conversione VB static-local che produce l’inizializzatore del campo. Confronta l’output generato con l’inizializzazione lazy prevista e verifica il comportamento per accessi ripetuti e concorrenti alla proprietà; il lavoro è completato quando l’inizializzatore viene rinviato fino all’utilizzo della proprietà e il codice convertito mantiene la semantica richiesta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

VB -> C#
VB.Net input code
Public Class VisualBasicClass

    Sub Main
        Console.WriteLine(Example)
    End Sub

    Public Readonly Property Example As Long 
        Get
            Static StaticVar As Long  = LongRunningFunction
            Return StaticVar -1 
        End Get
    End Property

    Public Shared Function LongRunningFunction As Long 
        Dim i  As Long = 1
        While i<10000000000
            i +=1
        End While  
    Return i
    End Function 
End Class
Erroneous output

public partial class VisualBasicClass
{
    public void Main()
    {
        Console.WriteLine(Example);
    }

    private long _Example_StaticVar = LongRunningFunction();

    public long Example
    {
        get
        {
            return _Example_StaticVar - 1L;
        }
    }

    public static long LongRunningFunction()
    {
        long i = 1L;
        while (i < 10000000000L)
            i += 1L;
        return i;
    }
}
Expected output

public partial class VisualBasicClass
{
    public void Main()
    {
        Console.WriteLine(Example);
    }

        private long _Example_StaticVar;
	private bool _Example_StaticVar_Init;

    public long Example
    {
        get
        {
			if (!_Example_StaticVar_Init) {
				_Example_StaticVar = LongRunningFunction();
				_Example_StaticVar_Init=true;
			}
            return _Example_StaticVar - 1L;
        }
    }

    public static long LongRunningFunction()
    {
        long i = 1L;
        while (i < 10000000000L)
            i += 1L;
        return i;
    }
}
Details
  • Website aka codeconverter.icsharpcode.net
  • Version in use: 8.4.7.0
  • Never saw it working
  • While Static variables aren't common, when using a static variable the lazy initialization is IMO essential. The "suggested" code isn't thread safe, unlike the VB code, but it's probably what most people would write as an equivalent. A more exact conversion of the Example property would be:
readonly Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag _Example_StaticVar_Init = new Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag();
 private long _Example_StaticVar;

public long Example
{
	get
	{
		bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag flag)
		{
			if (flag.State == 0)
			{
				flag.State = 2;
				return true;
			}
			else if (flag.State == 2)
			{
				throw new Microsoft.VisualBasic.CompilerServices.IncompleteInitialization();
			}
			else
			{
				return false;
			}
		}

		lock (_Example_StaticVar_Init)
		{
			try
			{
				if (InitStaticVariableHelper(_Example_StaticVar_Init))
				{
					_Example_StaticVar = LongRunningFunction();
				}
			}
			finally
			{
				_Example_StaticVar_Init.State = 1;
			}
		}
		return _Example_StaticVar;
	}
}

On one hand this is fairly close to an exact translation, on the other hand it's ugly. I would actually come down on the side of more exact translation (I do use it for the thread safety in a couple of places), but would understand if you felt it was too much, and the thread safety probably isn't all that important to most people (said without any basis other than a WAG).

Lingua principale
C#
Stelle
918
Fork
244
Merge medio
1g 10h
PR unite (30g)
29

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di icsharpcode/CodeConverter

Tutte le issue di icsharpcode/CodeConverter

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.