0

I have class, its constructor takes SemaphoreSlim and it must be static(if else I should throw an exception). How can I check that incoming "semaphore" parameter is static? I have tried to look through its Type, but there's no IsStatic field. The code is below

protected CacheService(SemaphoreSlim semaphore, IMemoryCache memoryCache)
{
    _semaphore = semaphore;
    _memoryCache = memoryCache;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Probably this https://stackoverflow.com/questions/63383681/how-to-check-if-string-matches-the-name-from-any-variable-in-a-static-class might help. – Bismark Asiedu Asante Aug 01 '22 at 09:13
  • Why does it have to be static? If you want to ensure the same semaphore is used for all CacheServices, why not declare a private static field? What is the actual goal? – JonasH Aug 01 '22 at 09:23
  • I think you are looking for the Singleton design pattern here an [example](https://www.c-sharpcorner.com/UploadFile/8911c4/singleton-design-pattern-in-C-Sharp/) – Marco Beninca Aug 01 '22 at 09:52
  • @JonasH this is the constructor of an abstract class with generic type parameter. One inheritor cache service calls another cache service and they both use the same static semaphore(1,1). That's why they must have different static semaphores and the only way I see is to put static semaphores in :base() constructor. It works but I don't know how to check this condition, for such future cases – emeraldloop Aug 01 '22 at 10:55
  • To me that sounds like something solved by making the sensitive constructors private, and use code-reviews and/or unit tests to verify that the code works as intended. As long as the public methods are foolproof I would accept some internal ugliness. – JonasH Aug 01 '22 at 12:02

1 Answers1

1

An object is never "static", by definition. What you're really asking here is, presumably, "is the argument being passed in coming from a static field". There isn't really any way of answering that at runtime, but this could be validated in a custom Roslyn code analyzer. Typically you would create your own [MustBeStatic] attribute that you would apply to the semaphore parameter, and have the analyzer look for this attribute (by full name, not by type - simply because of how the compiler API works), and kick in some logic that would analyze call-sites and check the expression being passed in. This would (by necessity) result in a build-time warning or error, not a runtime exception - since Roslyn analyzers happen as part of the build.

So: it is absolutely possible (I've done very similar things). However! This is an absolute ton of work, using specialized and complicated APIs (the Roslyn API is powerful but very non-trivial). Honestly, unless you genuinely think you're going to use this a lot, I think this is better solved in code-review and testing.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • It's also worth noting that, even if the calling argument was a static field, the same object could still be assigned to any number of other static, instance and local variables. – John Aug 01 '22 at 09:25