I'm creating a DB connection class in C# (.NET 3.5). It will be used by many developers, and I would like to ensure programmatically (by raising an exception) that the class is being instantiated within a "using statement". Is there anyways to check that?
Asked
Active
Viewed 48 times
0
-
Do you mean C# 3, or .NET 3.5? There's no such thing as C# 3.5. – Jon Skeet Jan 29 '14 at 16:53
-
1Does it _have_ to be in a `using` statement? What if the dev chooses to use a `finally` block with a `Dispose()` call? – D Stanley Jan 29 '14 at 16:54
-
I think you're mixing stylistic concerns with design concerns when they really are two separate things. Check out StyleCop to help enforce stylistic rules. Another option is Code Coverage utility in VS. – doogle Jan 29 '14 at 18:16
-
What if the user of this type needs it to exist beyond the scope its declared in? While being able to use a `using` is certainly the most common and typical use case, it's certainly not representative of *all* valid use cases. – Servy Jan 29 '14 at 18:16
-
since the using block is pure syntactic sugar, you can't enforce its use. As a library producer you have to get used to people who refuse to heed your warnings ("it will hurt if you do this" "Hey how come when I do this it hurts?" – plinth Jan 29 '14 at 18:17