同一应用程序中的多个静态变量范围?
在同一应用程序中设置多个静态作用域的最佳方法是什么? 我有用作访问数组的包装器的结构。
下面是一个示例:
class FooClass{
static int[] BarArray;
}
struct FooStruct{
public int BarArrayIndex;
public int BarArrayValue {
get { return FooClass.BarArray[BarArrayIndex]; }
set { FooClass.BarArray[BarArrayIndex] = value; }
}
}
出于性能原因,我不想在 FooStruct 的每个实例中存储对 BarArray 的引用,因此我将数组声明为静态。 但是,将来我可能必须同时使用多个不同的 BarArray(其中结构的不同实例应指向不同的数组)。有没有一种方法可以实现这一目标,而无需在结构的每个实例中存储额外的引用,也不使用静态变量? 如果不是,那么使用多个静态实例同时使整个应用程序对最终用户来说就像“一个应用程序”的最佳方法是什么?
What is the best way to set up multiple static scopes within the same application?
I have structs that serve as wrappers over accessing an array.
Here's an example:
class FooClass{
static int[] BarArray;
}
struct FooStruct{
public int BarArrayIndex;
public int BarArrayValue {
get { return FooClass.BarArray[BarArrayIndex]; }
set { FooClass.BarArray[BarArrayIndex] = value; }
}
}
For performance reasons, I don't want to store a reference to BarArray in every instance of FooStruct, hence I declared the array static.
However, it's possible that in the future I'll have to work with multiple different BarArrays at the same time (where different instances of the struct should point into different arrays). Is there a way to achieve that without having to store an additional reference in every instance of the structs and not using a static variable neither?
If not, what's the best way to use multiple static instances while making the whole application feel as "one application" for the end-user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您似乎认为持有对数组的引用意味着复制数组..即您的结构的每个实例都将包含数组的副本?事实并非如此。所有结构将包含对数组的引用......一个指针。内存中只会存在该数组的一个实例。我不确定这是否能为您带来任何性能分数。
You seem to think that holding a reference to an array means copying the array .. ie that each instance of your struct would contain a copy of the array? This is not the case. All the struct would contain is a reference to the array ... a pointer. There would only exist one instance of the array in memory. I'm not sure this is gaining you any performance points.
你不能。静态的要点是在整个应用程序中拥有一个实例。
请查看依赖注入。它应该完美地满足您的用例,并且是处理此类问题的首选方法。
You can't. The point of static is to have one instance over the whole application.
Have a look at Dependency Injection instead. It should fulfill your usecase perfectly fine and is the prefered way of handling such a problem.
然后使它们成为类对象。然后你只需要传递一个引用,你就可以将引用添加到数组中而不会受到惩罚。 (不,在堆上使用 1M 小对象不是性能问题)。
但我严重怀疑复制小结构是由分析器挑选出来的。听起来您正在猜测瓶颈在哪里。
Then make them class objects. Then you only have to pass a reference around and you can add a ref to the array without penalty. (And No, using 1M small objects on the heap is not a perfromance issue).
But I seriously doubt that copying small structs was singled out by a profiler. It sounds like you're guessing where the bottleneck is.
这是单例模式的概括。
顺便说一句,每个 FooStruct 实例保存 FooClass 的公共实例的性能损失绝对是微不足道的。
This is a generalization of the Singleton pattern.
By the way, the performance penalty for each instance of FooStruct to hold on to a common instance of FooClass is absolutely trivial.
此时您能做的最好的事情就是向 FooClass 添加一个工厂方法,负责返回 BarArray 的实例。
现在,实现此方法以返回静态对象。如果将来您决定更改创建数组的策略,只需重新实现工厂方法即可。
The best you could do at this moment is to add a factory method to the FooClass, responsible for returning an instance of the
BarArray
.For now, implement this method to return a static object. If somewhere in future you decide to change the policy of creating the array, you just reimplement the factory method.
如果您只想拥有多个静态变量,请为每个上下文使用新的 AppDomain。
但我不确定这是否适合您的问题(请参阅其他答案)。
If you just want to have multiple static Variables use a new AppDomain for every Context.
But i'm not sure if this is the right soultion for your Problem (See other Answers).