public class Class1
    {
        public string File1
        {
            get { return _str; }
            set 
            {
                //在这里调用Class2的Method1方法
                Class2.Method1();                _str = value;
            }
        }
        private string _str;
    }    public class Class2
    {
        public static void Method1()
        { 
            //我想在这里知道自己(Method1)被谁调用了。
            //想得到的信息是:类名:Class1;属性名:File1.
        }
    }请教高手,可以实现吗?

解决方案 »

  1.   

    TO: he_8134(只有星星伴明月)
    如何实现?
      

  2.   

    传参给Method1
    把想告诉它的都告诉它
      

  3.   

    set 
                {
                    //在这里调用Class2的Method1方法
                    Class2.Method1(this);                _str = value;
                }
    ////////////////////////////////
        public class Class2
        {
            public static void Method1(object o)
            { 
                Type t = typeof(o);
                
            }
        }属性实例的类型忘记了,也想传类型那样传属性的信息..我查查看先..
      

  4.   

    属性那里传个字符串就行了~~~~得到一个类的Type对象,根据属性名可以得到该属性的了~~        public static void Method1(object o,string p)
            { 
                Type t = typeof(o);
                System.Reflection.PropertyInfo pro =t.GetProperty(p);
            }
      

  5.   

    既然你有这个要求,那就简单多了,不行.可以修改代码你就修改,为什么偏偏要折磨自己,多了解.net框架,少走些弯路~~`
      

  6.   

    取巧吧~~~用一个全局变量做标记~~前提是单线程情况下
    public static object oo;
    public class Class1
        {
            public string File1
            {
                get { return _str; }
                set 
                {
                    oo=this;
                    Class2.Method1();                _str = value;
                }
            }
            private string _str;
        }    public class Class2
        {
            public static void Method1()
            { 
                //根据oo做判断~~~
            }
        }
      

  7.   

    如果可以进运行时,或许会有帮助 :D不然GC怎么知道当前谁可以被咔嚓掉?
      

  8.   

    public class Class1
        {
            public string File1
            {
                get { return _str; }
                set 
                {
                    Class2.oo=this;
                    Class2.Method1();                _str = value;
                }
            }
            private string _str;
        }    public class Class2
        {
            public static object oo;//最好做成一个属性~~
            public static void Method1()
            { 
                //根据oo做判断~~~
            }
        }
      

  9.   

    System.Reflection.MethodInfo.GetCurrentMethod()
    可以获取当前执行的方法。
      

  10.   

    public class Class1
        {
            public string File1
            {
                get { return _str; }
                set 
                {
                    //在这里调用Class2的Method1方法
                    Class2.Method1(System.Reflection.MethodInfo.GetCurrentMethod());                _str = value;
                }
            }
            private string _str;
        }    public class Class2
        {
            public static void Method1(System.Reflection.MethodBase caller)
            { 
                //我想在这里知道自己(Method1)被谁调用了。
                //想得到的信息是:类名:Class1;属性名:File1.
                Console.WriteLine("类名: {0}", caller.DeclaringType.Name);
                Console.WriteLine("方法名: {1}", caller.Name);
            }
        }-------------------
    补充:
    这个办法只能获取到执行者的方法名,如果是属性比如你的例子,得到的是set_File1,而不是File1。现在还没有找到什么办法来直接获得属性名,不过暂时可以先按字符串的办法来处理。Good luck!
      

  11.   

    在Debug下的调用堆栈里的信息是如何得到的呢?
      

  12.   

    通过反编译可以看出MethodBase.GetCurrentMethod()最终是调用System.RuntimeMethodHandle的_GetCurrentMethod方法获取当前调用方法的,这个方法的声明如下:
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern unsafe void* _GetCurrentMethod(ref StackCrawlMark stackMark);
    参数StackCrawlMark是一个枚举,它有以下几个值:
    [Serializable]
    internal enum StackCrawlMark
    {
        LookForMe,
        LookForMyCaller,
        LookForMyCallersCaller,
        LookForThread
    }
    可惜这些东西都是internal的,因此无法直接调用,除非用反射。
      

  13.   

    绕了一个大圈,终于找到正常的办法了,可以使用System.Diagnostics.StackTrace来获取调用者的MethodBase,不过仍旧存在之前说过的问题。