Saturday, 15 February 2014

c# - Prevent “Access to a static member of a type via a derived type” when deriving classes having structs with string constants -



c# - Prevent “Access to a static member of a type via a derived type” when deriving classes having structs with string constants -

i'm wrapping forgerock rest api implementation in c#. implementation layered. there base of operations layer on forgerock level, gets extended each of products (like openam, opendj, etc).

parts of api layers definitions of http parameter names (and restriction on values) i've solved hierarchy of knownparameter classes, each having structs defining parameters in structs each having 1 name , potentially methods or nested values struct limiting or converting values.

the code base of operations limited 1 product (and ancestor layer: forgerock).

from perspective, want avoid “access static fellow member of type via derived type” warning generated resharper.

having classes same names help on 1 hand (they same thing, different namespace perspective, compiles , works fine), on other hand: after renaming forgerock.knownparameters forgerock.knownparametersbase resharper isn't happy it. think in confined case, ok have kind of naming

how can avoid warnng while still benefiting , , still benefit inheritance?

example code (also on-line more urls referencing right bits of documentation) in 4 pieces.

main programme exhibiting resharper warning:

using system.collections.specialized; using forgerock.openam; namespace accesstoastaticmemberofatypeviaaderivedtype { class programme { static void main() { new namevaluecollection { {knownparameters.prettyprint.name, knownparameters.prettyprint.values.true}, {knownparameters.action.name, knownparameters.action.values.logout} }; } } }

forgerock namespace base of operations knownparameters class:

namespace forgerock { public class knownparameters { public struct prettyprint { public const string name = "_prettyprint"; public struct values { public const string false = "false"; public const string true = "true"; } } } }

forgerock.openam namespace descendant of knownparameters class.

namespace forgerock.openam { public class knownparameters : forgerock.knownparameters { public struct action { public const string name = "_action"; public struct values { public const string logout = "logout"; } } } }

forgerock.opendj namespace descendant of knownparameters class.

namespace forgerock.opendj { public class knownparameters : forgerock.knownparameters { public struct queryfilter { public const string name = "_queryfilter"; } } }

i think in confined case, ok have kind of naming.

well, disagree. it's bad readability when same name means different things within programme - it's worse (imo) when 2 names related inheritance. if want utilize approach, there 2 options:

find way of specifying forgerock.knownparameters when want utilize members, e.g.

new namevaluecollection { {forgerock.knownparameters.prettyprint.name, forgerock.knownparameters.prettyprint.values.true}, {knownparameters.action.name, knownparameters.action.values.logout} };

or utilize using alias:

using baseknownparameters = forgerock.knownparameters; ... new namevaluecollection { {baseknownparameters.prettyprint.name, baseknownparameters.prettyprint.values.true}, {knownparameters.action.name, knownparameters.action.values.logout} };

disable r# warning, if you're happy you're accessing fellow member of forgerock.knownparameters via forgerock.openam.knownparameters, using asciiencoding.utf8

as aside, suggest using static classes instead of structs. @ moment, write:

var x = new knownparameters.prettyprint.values();

... want legal? (it won't using static class.) might consider using enums.

c# resharper

No comments:

Post a Comment