VB -> C#: Static variables are supposed to be lazy initialized inside method/property
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Tái hiện việc chuyển đổi VB-to-C# từ issue trên trang web CodeConverter, sau đó lần theo đường dẫn chuyển đổi VB static-local tạo ra field initializer. So sánh đầu ra được tạo với việc khởi tạo lazy dự kiến và xác minh hành vi khi truy cập thuộc tính lặp lại và đồng thời; được xem là hoàn tất khi initializer được trì hoãn cho đến khi thuộc tính được sử dụng và mã đã chuyển đổi vẫn giữ nguyên ngữ nghĩa cần thiết.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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).
- Ngôn ngữ chính
- C#
- Star
- 918
- Fork
- 244
- Merge trung bình
- 1 ngày 10 giờ
- Pull request đã merge (30 ngày)
- 29
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của icsharpcode/CodeConverter
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
icsharpcode/CodeConverter#1271 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 52/100
icsharpcode/CodeConverter#1243 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
VB -> C#: Enum Values as IntegerCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởGood for coding agent VB -> C#
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
icsharpcode/CodeConverter#1212 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Good for coding agent VB -> C#
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
icsharpcode/CodeConverter#1211 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
C# -> VB
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 30/100
icsharpcode/CodeConverter#1171 · 7 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của icsharpcode/CodeConverter
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
umbraco/Umbraco.Automate#612 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
崩溃日志只按 CRLF 分行Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
PCL-Community/PCL-CE#3872 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
type:task ux
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
elsa-workflows/elsa-foundation#5253 ·
Maintainer thường phản hồi trong vòng 1 ngày