VB -> C#: Static variables are supposed to be lazy initialized inside method/property
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 45/100
Piste de recherche
Reproduisez la conversion VB-to-C# de l’issue sur le site web CodeConverter, puis suivez le chemin de conversion VB static-local qui produit l’initialiseur de champ. Comparez la sortie générée avec l’initialisation différée attendue et vérifiez le comportement lors d’accès répétés et concurrents à la propriété ; c’est terminé lorsque l’initialiseur est différé jusqu’à l’utilisation de la propriété et que le code converti préserve la sémantique requise.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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).
- Langage dominant
- C#
- Étoiles
- 918
- Forks
- 244
- Merge moyen
- 1 j 10 h
- PR mergées (30 j)
- 29
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de icsharpcode/CodeConverter
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
icsharpcode/CodeConverter#1271 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
enhancement
Difficulté 4/5 3-5 jours Accessibilité débutants 52/100
icsharpcode/CodeConverter#1243 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
VB -> C#: Enum Values as IntegerPeut-être à nouveau libre Une pull request pour cette issue a été fermée sans être fusionnée. OuverteGood for coding agent VB -> C#
Difficulté 3/5 1-2 jours Accessibilité débutants 45/100
icsharpcode/CodeConverter#1212 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Good for coding agent VB -> C#
Difficulté 3/5 1-2 jours Accessibilité débutants 48/100
icsharpcode/CodeConverter#1211 ·
Les mainteneurs répondent en général sous 1 jour
-
C# -> VB
Difficulté 4/5 3-5 jours Accessibilité débutants 30/100
icsharpcode/CodeConverter#1171 · 7 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de icsharpcode/CodeConverter
Issues similaires
-
DXF with empty names failsOuvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 3 jours
-
bug python
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
ilia-sokolov/OfficeAgent.NET#89 ·
Les mainteneurs répondent en général sous 13 jours
-
documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
juandresrodca/AdminForge#66 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
umbraco/Umbraco.Automate#612 ·
Les mainteneurs répondent en général sous 1 jour