Wednesday, 15 May 2013

c# - Static getter-only property in class VS const -



c# - Static getter-only property in class VS const -

i have base of operations type , couple of types derived base of operations type. base of operations type has property friendlyname contains info constant every derived type (so not on per-instance level, on type level)

how implement this? right now, have (which doesn't want)

public class baseclass { protected static string friendlyname; public static string friendlyname { { homecoming friendlyname; } } } public class derivedclassone : baseclass { public derivedclassone() { friendlyname = "a super duper derived class"; } } public class derivedclasstwo : baseclass { public derivedclassone() { friendlyname = "yet derived class"; } }

is there way want? or way things or should rather utilize const string same name every derived type?

edit: (hopefully) clear things bit. have mvc application. class in question model. in view, display list of type @model ienumerable<basetype>. every single item of collection want show descriptive text containing info current type, not typeof(item).tostring() "friendly" description.

for edit. i'll create custom attribute, or utilize existing on in .net framework , decorate class attibute.

[friendlyname("hey cool class")] public class derivedclassone : baseclass { ... }

and forth types.

then utilize type.getcustomattributes read attribute. simple that.

c# class inheritance

No comments:

Post a Comment