c# - Trying to get a string that represents a class heirachy -
class base { } class a: base { } class b:a { } i want able instance of b string "base.a.b"
of course do
class b:a { const string name = "base.a.b"; } but thats kind of fragile, if change things have change in many places
i started out with
class base { protected string name {get{return typeof(base).name;}}; } with dea of each class hierarchy level calling base.name method. again have had name base twice (admittedly fail @ compile time if forget) still seems fragile.
same code others, went effort, going damn post (the difference didn't want ".object" stuck on end).
public class class1 { } public class class2 : class1 { } public class class3 : class2 { } private void button1_click(object sender, eventargs e) { class3 c3 = new class3(); console.writeline(typetostring(c3.gettype())); } private string typetostring(type t) { if (t == null) return ""; if ((t.basetype == null) || (t.basetype == typeof(object))) return t.name; return typetostring(t.basetype) + "." + t.name; }
Comments
Post a Comment