我在字典中有动态项属性,可以包含单个或字符串或其他2个自定义类。
现在我将值存储在一个具有所有这些变量的未初始化变量的类中,另一个变量则表示根据请求获取哪种类型。 我不喜欢它,因为它相当笨重,似乎浪费了内存(因为大多数时候存储的值是单个)。
我想我可以在单个对象变量类型中持有任何这些,但我不知道对此会有什么样的惩罚,如果有的话。 我应该继续自己管理类型还是让vb搞清楚?
I have dynamic item attributes in a dictionary that could hold either a single or a string or 2 other custom classes.
Right now i store the value in a class that has uninitialized variables for all of these and another variable to say which type to get upon request. I don't like it because it's rather clunky and seems to waste memory (since most of the time the value stored is a single).
I figured i could hold any of these in a single object variable type but i don't know what kind of penalties to expect from this, if any. Should i continue with managing the types myself or let vb figure it out?
最满意答案
你应该担心VB.NET中的Object类型是后期绑定。 如果在Object变量上调用方法(除了那些属于Object类型的方法,比如ToString),vb运行时必须使用反射来查找并在执行时调用确切类型的正确方法。 如果对变量使用特定类型,那么当您的代码是JIT编译时,该查找只会发生一次。 我会说后期绑定的开销足够大,你应该尽可能避免它。
但是,这不适用于检查对象的类型并分配给具有更具体类型的变量。 因此,如果你弄清楚你拥有什么类型的对象并在调用方法之前将它分配给正确类型的变量,你应该没问题。 这样做有一点开销,但它可能并不比你已经做的更糟糕。
避免后期绑定的另一个原因是它阻止编译器进行类型检查。
The main thing you should worry about with the Object type in VB.NET is late-binding. If you call a method on an Object variable (except for those methods that are part of the Object type, such as ToString), the vb runtime has to use reflection to find and call the correct methods on the exact type when they're executed. If you use a specific type for your variable, that lookup will only occur once, when your code is JIT-compiled. I would say that the overhead of late-binding is significant enough that you should avoid it when possible.
However, that doesn't apply to checking the object's type and assigning to a variable with a more specific type. So if you figure out what type of object you have and assign it to the right type of variable before calling methods on it, you should be ok. There is a little bit of overhead to doing that, but it's probably no worse than what you're already doing.
Another reason to avoid late-binding is that it prevents the compiler from doing type checking.
vb.net中对象类型的效率如何?(how efficient is Object type in vb.net?)我在字典中有动态项属性,可以包含单个或字符串或其他2个自定义类。
现在我将值存储在一个具有所有这些变量的未初始化变量的类中,另一个变量则表示根据请求获取哪种类型。 我不喜欢它,因为它相当笨重,似乎浪费了内存(因为大多数时候存储的值是单个)。
我想我可以在单个对象变量类型中持有任何这些,但我不知道对此会有什么样的惩罚,如果有的话。 我应该继续自己管理类型还是让vb搞清楚?
I have dynamic item attributes in a dictionary that could hold either a single or a string or 2 other custom classes.
Right now i store the value in a class that has uninitialized variables for all of these and another variable to say which type to get upon request. I don't like it because it's rather clunky and seems to waste memory (since most of the time the value stored is a single).
I figured i could hold any of these in a single object variable type but i don't know what kind of penalties to expect from this, if any. Should i continue with managing the types myself or let vb figure it out?
最满意答案
你应该担心VB.NET中的Object类型是后期绑定。 如果在Object变量上调用方法(除了那些属于Object类型的方法,比如ToString),vb运行时必须使用反射来查找并在执行时调用确切类型的正确方法。 如果对变量使用特定类型,那么当您的代码是JIT编译时,该查找只会发生一次。 我会说后期绑定的开销足够大,你应该尽可能避免它。
但是,这不适用于检查对象的类型并分配给具有更具体类型的变量。 因此,如果你弄清楚你拥有什么类型的对象并在调用方法之前将它分配给正确类型的变量,你应该没问题。 这样做有一点开销,但它可能并不比你已经做的更糟糕。
避免后期绑定的另一个原因是它阻止编译器进行类型检查。
The main thing you should worry about with the Object type in VB.NET is late-binding. If you call a method on an Object variable (except for those methods that are part of the Object type, such as ToString), the vb runtime has to use reflection to find and call the correct methods on the exact type when they're executed. If you use a specific type for your variable, that lookup will only occur once, when your code is JIT-compiled. I would say that the overhead of late-binding is significant enough that you should avoid it when possible.
However, that doesn't apply to checking the object's type and assigning to a variable with a more specific type. So if you figure out what type of object you have and assign it to the right type of variable before calling methods on it, you should be ok. There is a little bit of overhead to doing that, but it's probably no worse than what you're already doing.
Another reason to avoid late-binding is that it prevents the compiler from doing type checking.
发布评论