reflection - C# Retrieving Classname in a static method -


example:

namespace myprogram.testing {      public class test1     {         public void testmethod()         {             string actualtype = this.gettype().fullname.tostring();             return;         }          public static string getinheritedclassname()         {             return system.reflection.methodbase.getcurrentmethod().reflectedtype.fullname;         }      }      public class test2 : test1     {     }      public class test3     {        string test2classname = test2.getinheritedclassname();     } } 

anyway, want return "myprogram.testing.test2" instead test2.getinheritedclassname() returns "myprogram.testing.test1". have put static class return (if possible)?

the code that's printing out type base-class method. except rare reflection scenarios such provide above, execution wouldn't affected whether method called using derived type or base type, system makes no distinction.

you could, however, around defining generic base type:

     class classnametesterbase<t>where t:classnametester<t>     {         public static string getname() { return (typeof(t)).name; }     } 

and defining other types of interest:

     class classnametester1<t> : classnametesterbase<t> ...     class classnametester2<t> : classnametester1<t> ... 

one may if desired define leaf classes:

     class classnametester1 : classnametester1<classnametester1> { }     class classnametester2 : classnametester2<classnametester2> { } 

one slight caveat here classnametester2 derives innards from classnametester1<t> not substitutable having classnametester1<classnametester1>; if it's being used static class, though, shouldn't problem.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -