动态对象或属性? [英] Dynamic objects or properties?

查看:38
本文介绍了动态对象或属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个含糊不清的问题,我深表歉意,但我不确定如何继续.

I apologize for the vague question, but I'm unsure how to proceed.

我需要的是像类对象一样工作的东西,它具有用于存储数据的各种字段和属性.
但是,由于在编译时并非所有字段/属性都是已知的,因此我还需要能够在运行时添加和使用新的字段/属性.

What I need is something that works like a class object with various fields and properties for storing data.
But, since not all the fields/properties are known at the compile time, I also need to be able to add and use new fields/properties in runtime.

这些对象稍后将排列在列表中,按这些字段/属性中的值排序并绑定到 WPF 控件.

These objects would later be arranged in lists, sorted by the values in those fields/properties and bound to WPF controls.

现在我只使用:具有各种属性的类对象,但我开始遇到问题,我需要添加更多字段/属性.

For now I'm using just that: class objects with various properties, but I'm starting to run into problems, where I need to add more fields/properties.

有什么我可以在 vb.net 中使用的东西吗?

Is there something I could use to achieve this in vb.net?

好的,我会尝试说明.

目前我有这样的事情.
假设我已经定义了一个这样的对象

Currently I have something like this.
Let's say I have defined an object like this

Public Class DataEntry
    Public Property Name As String
    Public Property Type As String
    Public Property Msc As Integer
End Class

如果我知道一开始我将拥有的所有属性,那效果很好.如果我突然需要添加另一个属性,我会遇到问题:

That works fine if I know all the properties I will have at the start. I run into problems if I suddenly need to add another property:

Public Class DataEntry
    Public Property Name As String
    Public Property Type As String
    Public Property Msc As Integer
    Public Property AdditionalDescription As String
End Class

当然,我可以重新编译整个过程,但由于我不知道最终需要的所有可能的属性,我想知道,也许有办法从运行时实现这一点?

Of course, I could recompile the whole thing, but since I don't know all the possible properties I will be needing in the end, I was wondering, maybe there is a way to achieve this from runtime?

或者我应该只使用复杂的数组堆而不是自定义对象?

Or should I just use complicated heap of arrays instead of custom objects?

推荐答案

在运行时无法向类添加新属性.

It's not possible to add new properties to a class during run time.

如果您不想提前向类添加您可能不会使用的属性,那么您可以改为使用 字典 用于存储直到运行时您才知道的属性".

If you don't want to add properties to the class ahead of time which you might not use, then you could instead use a dictionary to store 'properties' which you're not aware of until run time.

Public Property RunTimeProperties As New Dictionary(Of String, Object)

保存Object"类型值的字典几乎可以存储任何内容.字符串、数组、列表等

A dictionary which holds values of type 'Object' can store just about anything. Strings, Arrays, Lists etc.

RunTimeProperties.Add("Length", 100)
RunTimeProperties.Add("Height", 200)
RunTimeProperties.Add("MiddleName", "Rupert")
RunTimeProperties.Add("SiblingsNames", New String() {"John", "Sarah", "Michael"})

您可以使用 TryGetValue 从字典中获取值的方法.

You can use the TryGetValue method to get the values out of the dictionary.

Dim value As Object

If RunTimeProperties.TryGetValue("Length", value) Then
    ' Length was found in the dictionary
Else
    ' Length was not found in the dictionary
End If

这篇关于动态对象或属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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