VB -> C#: Static variables are supposed to be lazy initialized inside method/property
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
調査の方向性
CodeConverter ウェブサイトで issue にある VB-to-C# 変換を再現し、その後、フィールド初期化子を生成する VB static-local 変換パスを追跡します。生成された出力を期待される遅延初期化と比較し、プロパティへの反復アクセスおよび同時アクセス時の動作を確認します。完了条件は、プロパティが使用されるまで初期化子が遅延され、変換後のコードが必要なセマンティクスを維持していることです。
索引モデルが issue の本文から書いたものです。
説明
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).
- 主要言語
- C#
- スター
- 918
- フォーク
- 244
- 平均マージ
- 1日 10時間
- マージ済み PR(30日)
- 29
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
icsharpcode/CodeConverter のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
icsharpcode/CodeConverter#1271 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
icsharpcode/CodeConverter#1243 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
VB -> C#: Enum Values as Integer再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンGood for coding agent VB -> C#
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
icsharpcode/CodeConverter#1212 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Good for coding agent VB -> C#
難易度 3/5 1〜2日 初心者へのやさしさ 48/100
icsharpcode/CodeConverter#1211 ·
メンテナーはふだん 1 日以内に返信
-
C# -> VB
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
icsharpcode/CodeConverter#1171 · コメント 7 件 ·
メンテナーはふだん 1 日以内に返信
icsharpcode/CodeConverter の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
umbraco/Umbraco.Automate#612 ·
メンテナーはふだん 1 日以内に返信
-
崩溃日志只按 CRLF 分行オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
PCL-Community/PCL-CE#3872 ·
メンテナーはふだん 1 日以内に返信
-
type:task ux
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
elsa-workflows/elsa-foundation#5253 ·
メンテナーはふだん 1 日以内に返信