public class 妓女
{
    public static string 上()
    {
        System.Diagnostics.StackFrame frame = new System.Diagnostics.StackFrame(0);
        return frame.GetMethod().ReflectedType.Name;
    }
}
public class 嫖客A : 妓女
{ }
public class 嫖客B : 妓女
{}
static void Main()
 {
    Console.WriteLine(嫖客A.上());
    Console.Read();
 }public class 妓女
{
    public static string 上()
    {
        return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name;    
    }
}
public class 嫖客A : 妓女
{ }
public class 嫖客B : 妓女
{}
static void Main()
 {
    Console.WriteLine(嫖客A.上());
    Console.Read();
 }
=====================================================
上面对2种方法都不行,返回的都不是"妓女",讨论下怎么才能让"妓女"知道是谁"上"了它!!!

解决方案 »

  1.   

    搞错了,上是嫖客的事,和妓女什么关系?被上才是妓女的事。
    另外就是:编译器会把继承类的静态调用编译成直接调用基类的。也就是 嫖客A.上 在IL中是 妓女A.上,搞不清楚的。
      

  2.   

    这个类的继承关系本身就是不对的,因为嫖客A和嫖客B都不是妓女!没有is-a的关系
      

  3.   

    lz想得到类似js中的caller的对象。
      

  4.   


    class Program
        {
            public class Hooker
            {
                public virtual string Visit()
                {
                    return "Hooker";
                }
            }
            public class WhoremasterA : Hooker
            {
                public override string Visit()
                {
                    return "WhoremasterA";
                }
            }
            public class WhoremasterB : Hooker
            {
                public override string Visit()
                {
                    return "WhoremasterB";
                }
            }        static void Main(string[] args)
            {
                Hooker hookerA = new WhoremasterA();
                Hooker hookerB = new WhoremasterB();
                Console.WriteLine(hookerA.Visit());
                Console.WriteLine(hookerB.Visit());
                Console.Read();
            }
        }
      

  5.   

    不要忘了this就是指代当前对象:        public class 妓女
            {
                public string 上()
                {
                    return this.GetType().Name;
                }
            }
            public class 嫖客A : 妓女
            {        }
            public class 嫖客B : 妓女
            {        }        static void Main(string[] args)
            {
                嫖客A a = new 嫖客A();
                Console.WriteLine(a.上());
                嫖客B b = new 嫖客B();
                Console.WriteLine(b.上());
                Console.Read();
            }
    /*
    输出:
    嫖客A
    嫖客B*/