Is static context always single in C#?
Asked Answered
M

3

53

I have a library that has a static field inside. I want to create an app and reference this library so I'd have two instances of this static field. .Net runtime does not allow to reference the same library twice, but I wonder is it possible to overcome this limitation?

I'm not allowed to change the library, but I can copy/rename it.

Mikesell answered 30/1, 2015 at 11:58 Comment(0)
E
85

That's not as crazy as you think. In fact, you can achieve this using AppDomains.

Each AppDomain has its own storage location for static variables. So you can just create a second AppDomain in your process, and communicate between them using an object that inherits from MarshalByRefObject like in this MSDN example.

Endocranium answered 30/1, 2015 at 12:1 Comment(3)
I liked your answer, could you please provide some sweet LoC's :)Millardmillboard
@Jeremy the MSDN link from my answer has a complete cross App-domain communication example using proxies.Endocranium
That might not be crazy, but it sure rings a little "bad architecture alert" for me.Judsen
T
58

While Lucas' suggestion on AppDomains would work, alternatively you could create this effect using generics, as a class with different generic type arguments is treated as a different class, and therefore has its own static fields.

public class SomeClass<T>
{
    public static string SomeField;
}

Then:

SomeClass<int>.SomeField = "A";
SomeClass<string>.SomeField = "B";

Console.WriteLine(SomeClass<int>.SomeField);    // A
Console.WriteLine(SomeClass<string>.SomeField); // B

For example, the SomeClass<int> would be set in the library, whereas the SomeClass<string> would be your copy. Of course this would only work if you could change the library, or the library already used generics.

Trenton answered 30/1, 2015 at 12:6 Comment(4)
That's one of the most beautiful and horrific abuses of generics I've ever seen.Aubarta
@BradleyUffner wait until you see CRTP singletons in C++Desta
Yes, but those are different fields, not two instances of the same static field.Mayes
@immibis: The curiously recurring generics pattern is also quite handy in C#.Orthotropous
R
1

Both suggestions should work, but they are all terrific concerning architecture.

I a not aware about the context, but in your case is it possible to just create an aggregation class with a new property that is not static and just have two instances. This sound like a better way for me.

Everytime I have smart code, an alert starts in my head. Smart code is always too clever for a developer.

Rake answered 13/2, 2015 at 10:47 Comment(2)
I am completely aware of the consequences of this. I was explaining that in the start of the question, but moderator has removed this part due to offtopic. I have a 3rd party library I'm not able to change that has the functionality I need twice, but it is static so I can use it only once. I asked the creator of the library about this, and his response was to try to use hacks as it is too difficult to redesign. The approach with having two domains worked nice, and it looks quite easy and suitable (except that you have to manage lifetime), so I'm good with it.Mikesell
But you are right - it is important to pinpoint that you should not go with such approach unless you really really have toMikesell

© 2022 - 2024 — McMap. All rights reserved.