VB -> C#: Static variables are supposed to be lazy initialized inside method/property
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 45/100
Línea de trabajo
Reproduce la conversión de VB-to-C# del issue en el sitio web de CodeConverter y, después, sigue la ruta de conversión de VB static-local que produce el inicializador de campo. Compara la salida generada con la inicialización diferida esperada y verifica el comportamiento ante accesos repetidos y concurrentes a la propiedad; se considera terminado cuando el inicializador se difiere hasta que se usa la propiedad y el código convertido conserva la semántica requerida.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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).
- Lenguaje dominante
- C#
- Estrellas
- 918
- Forks
- 244
- Merge medio
- 1 d 10 h
- PR fusionados (30 d)
- 29
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de icsharpcode/CodeConverter
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
icsharpcode/CodeConverter#1271 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 52/100
icsharpcode/CodeConverter#1243 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
VB -> C#: Enum Values as IntegerQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. AbiertoGood for coding agent VB -> C#
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
icsharpcode/CodeConverter#1212 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
Good for coding agent VB -> C#
Dificultad 3/5 1-2 días Aptitud para principiantes 48/100
icsharpcode/CodeConverter#1211 ·
Los mantenedores suelen responder en 1 día
-
C# -> VB
Dificultad 4/5 3-5 días Aptitud para principiantes 30/100
icsharpcode/CodeConverter#1171 · 7 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de icsharpcode/CodeConverter
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
umbraco/Umbraco.Automate#612 ·
Los mantenedores suelen responder en 1 día
-
崩溃日志只按 CRLF 分行Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
PCL-Community/PCL-CE#3872 ·
Los mantenedores suelen responder en 1 día
-
type:task ux
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
elsa-workflows/elsa-foundation#5253 ·
Los mantenedores suelen responder en 1 día