c# - Comparing namespaces to see if they match -
I'm trying to compare namespaces to see if only in my method only throws the right exception With the right exceptions I mean the following:
- Exception from the same namespace.
- Exception from high namespace.
- Exception from the equivalent (and higher) system-namespace. Example:
- The method is in the namespace
MyNamespace.Collections.Generic
, so it can throw exceptions fromMyNamespace.Collections.Generic
. - The method is in the namespace
MyNamespace.Collections.Generic
, so it isMyNamespace.Collections
andMyNamespace
. - The method is in the namespace
MyNamespace.Collections.Generic
, so it can throw exceptions fromSystem.Collections.Generic
andSystem.Collections
andsystem
.
The first section is quite easy; One part of the same name space check number 3 works; Because
system
namespace is always correctFor other parts I tried the following:
string [exceptNamespaceSegments = exceptionNamespaceSegments. Except (classNamespaceSegments) ToArray (); If (except for appointments of names, the threshold == 1 & amp; amp; amp; amp; amp; amp; amp; amp; leave; Namespace section.Other default () == "system") return;
What determines that the name-space-section (archive, generic) is also used in class-namespace. If this is the case, then the right exception is thrown.
But this will not work for the case where the exception is in the namespace
System.Collections.Generic.Something
, becauseis not something
class-namespaceCome to think about it, it does not take into consideration the sequence. Then
System.Generic.Collections
will also be correct;In any way, can I write agar-statements in comparison to Segment every person?
- The method is in the namespace
Unless I am misinterpreting this question: you can try to do something like this , Which can find all permitted namespaces according to the norms.
Private static IEnumerable & lt; String & gt; GetAllowedNamespaces (type type) {const string system = "system"; String thisNamespace = type.Namespace; HashSet & LT; String & gt; HashSet = New Hashet & lt; String & gt; (); Hashset.Add (thisNamespace); Hashset.Add (system); Var parts = this namespace. Lamp ('.'); If (parts.Length & gt; 1) {string previous = string.Empty; Forrest (part part) {var current = string.IsNullOrEmpty (last)? Part: string.Format ("{0}. {1}", Previous, Part); Previous = current; Hashset.Add (current); } Back = string Empty; Leading (new part [] {system} .concact (parts. Skip (1))) var current = string .isial olive (last)? Part: string.Format ("{0}. {1}", Previous, Part); Previous = current; Hashset.Add (current); }} Return hashet; }
Then you can easily check the namespace fallout of the exception in this list, if there is no problem :)
Duplicate code is block, For a method to re-drill the principle can follow.
Comments
Post a Comment