公共共享只读成员在 VB.Net 中优化了吗? [英] Public Shared Readonly Member optimized out in VB.Net?

查看:20
本文介绍了公共共享只读成员在 VB.Net 中优化了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB.Net 中有这种奇怪的行为,我被困住了.目前我无法确定我是否遗漏了某些概念或者它是否是一个错误.

I have this weird behavior in VB.Net I am stuck with. At the moment I am not able to determine whether I am missing some concept or if it is a bug.

我有一个基类:

Public MustInherit Class BaseType(Of T As BaseType(Of T)) :
    Implements IEquatable(Of BaseType(Of T))
    Protected Sub New(key As Integer, value As List(Of String))
        LogManager.GetLogger("INFO").Info("Strings: " & vbTab & value.Count())
        If key <> -1 Then
            enumValues.Add(CType(Me, T))
        End If
    End Sub

    Protected Shared Function TypeFromStringBase(name As String) As T
        For Each b As T In enumValues
            LogManager.GetLogger("DEBUG").Info(b.Names(0))
            If b.Names.Contains(name) Then
                Return b
            End If
        Next

        Return TypeFromStringBase("Keine")
    End Function
End Class

还有一个继承它的类:

Public Class AnschlussType : Inherits BaseType(Of AnschlussType) :
    Implements IEquatable(Of AnschlussType)
    Public Shared ReadOnly Rund As New AnschlussType(1, {"Rund", "1"})
    Public Shared ReadOnly Flach As New AnschlussType(2, {"Flach", "2"})
    Public Shared ReadOnly Gewinde As New AnschlussType(3, {"Gewinde", "3"})
    Public Shared ReadOnly Kein As New AnschlussType(4, {"Kein", "None", "4"})

    Private Sub New(key As Integer, names As String())
        MyBase.New(key, names.ToList())
    End Sub

    Public Shared Function TypeFromString(name As String) As AnschlussType
        Return TypeFromStringBase(name)
    End Function
End Class

这是我不明白的奇怪部分.第一次调用 AnschlussType.TypeFromString("Some String") 时,VB 应该创建所有 Public Shared ReadOnly 成员.这会导致对 BaseType.New四次调用.然后每个调用都将自己的类型添加到 enumValues 列表中.

Here is the weird part I don't get. The first time you call AnschlussType.TypeFromString("Some String"), VB should create all the Public Shared ReadOnly members. This results in four calls to BaseType.New. Each of those calls then adds its own type to the enumValues List.

在所有这些初始化之后,最后将执行 AnschlussType.TypeFromString 调用.在那里它调用 TypeFromStringBase,它遍历我们之前填写​​的 enumValues 列表.

After all those initializations, finally, the AnschlussType.TypeFromString call will be executed. There it calls TypeFromStringBase which iterates over the enumValues List we filled right before.

这在调试模式下一切正常.

This all works fine in the DEBUG mode.

这是我不明白的奇怪部分.现在我尝试了 RELEASE 模式.enumValues.Count 将始终保持为 0.我认为这是因为记录器不打印任何内容,这意味着它不会迭代,这意味着它为零.因此,我进行了更多调查,并在 BaseType.New 中放入了一条日志记录语句.这根本不记录.这使我得出结论,根本没有执行 New.

Here is the weird part I don't get. Now I tried RELEASE mode. The enumValues.Count would always stay 0. I assumed this because the logger does not print anything, which means it doesn't iterate, which means it is zero. So I investigated a little more and put a logging statement into BaseType.New. And this does NOT log at all. This leads me to the conclusion that New is not executed at all.

让我强调一下,这一切在 DEBUG 模式下都很棒,并且与 Public Shared ReadOnly 成员在同一问题上的其他子类型一起使用.但这在 RELEASE 模式下不起作用.

Let me emphasize that this all works great in DEBUG mode and with other subtypes that have Public Shared ReadOnly members in the same matter. But this does not work in RELEASE mode.

有人对我可能遗漏的东西有什么提示吗?

Does anyone have a hint for me what I could be missing?

推荐答案

如果类中存在静态构造函数(第 10.11 节),则在执行该静态构造函数之前立即执行静态字段初始值设定项.否则,在第一次使用该类的静态字段之前的依赖于实现的时间执行静态字段初始值设定项.

If a static constructor (Section 10.11) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.

假设 VB 像 C# 一样工作,你的共享(即静态)字段没有被初始化,因为你没有使用它们.

Assuming VB works like C#, your shared (i.e. static) fields aren't being initialized because you haven't used them.

尝试创建一个共享构造函数.

Try creating a shared constructor.

这篇关于公共共享只读成员在 VB.Net 中优化了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆