我是.net的新手,正在寻找有关如何最好地创建只读对象结构的建议。这些结构在编译时布局,根本不会改变。我的对象看起来像这样:
Public Structure CatPair
Public code, name As String
End Structure
Public Structure Category
Public code, name As String
Public subcategories() As EpfPair
End Structure
Public ReadOnly categories() As Category = {
New Category With {.code = "Code1", .name = "cat title 1", .subcategories = Nothing
},
New Category With {.code = "Code2", .name = "cat title 2", .subcategories = Nothing
},
New Category With {.code = "Code3", .name = "cat title 3", .subcategories = Nothing
},
New Category With {.code = "Code4", .name = "cat title 4", .subcategories = {
New CatPair With {.code = "subC1", .name = "subcat title 1"},
New CatPair With {.code = "subc2", .name = "subcat title 2"},
New CatPair With {.code = "subc3", .name = "subcat title 3"}
}
},
New Category With {.code = "Code5", .name = "Cat title 5", .subcategories = Nothing
}
}
所以对象实际上是一个嵌套的常量表,其中一些有子类,有些没有。我的目标是:
答案 0 :(得分:1)
您提供的代码无法实现您的目标。要使对象不可变,你至少需要使对象的属性只读(没有setter),只需要在构造函数参数中传递所有内容。