簡體   English   中英

獲取創建對象的類的名稱

[英]Get the name of the class where the object was created

我有以下兩節課:

第一:

class Class1
 {
     private void Method1()
      {
          var obj=new TestClass();
          obj.TestMethod1();
      }
 }

第二個:

class TestClass
 {
     public void TestMethod1()
      {
           TestMethod2();
      }

      private void TestMethod2()
       {
           //get the calling class 
       }
 }

Class1.Method1調用TestClass.TestMethod1進而又調用TestClass.TestMethod2 ,我想在TestClass.TestMethod2內部獲取Class1的完全合格的類名。 我已經看到了此鏈接 ,但是我想我將獲得TestClass.TestMethod1作為方法名稱,並將TestClass作為類名稱。 如何獲得呼叫班級名稱?

沒有好的方法可以做到這一點。 您可以訪問堆棧框架(僅查看第二個框架,而不是第一個),但這既昂貴又脆弱。 您可以使用可選的調用方成員名稱屬性(從TestMethod1顯式)來獲得"Method1" ,而不是"Class1"部分的"Method1" 另一種選擇是顯式傳遞一個對象 (或只是名稱)。 例如:

  private void Method1()
  {
      var obj=new TestClass();
      obj.TestMethod1(this);
  }
  public void TestMethod1(object caller=null,
             [CallerMemberName] string callerName=null)
  {
       TestMethod2(caller??this,callerName??"TestMethod1");
  }

  private void TestMethod2(object caller=null,
             [CallerMemberName] string callerName=null)
  {
      string callerName = ((caller??this).GetType().Name) + "." + callerName
      //get the calling class 
  }

但我不得不承認這很丑陋

也許更好的是首先質疑您為什么需要此功能。

您不能通過構造函數將類型傳遞給第二個類嗎:

class Class1
{
    private void Method1()
    {
        Type t = typeof(Class1);
        var obj = new TestClass(t);
        obj.TestMethod1();
    }
}

class TestClass
{
    private Type _caller;

    public TestClass(Type type)
    {
        _caller = type;
    }
    public void TestMethod1()
    {
        TestMethod2();
    }

    private void TestMethod2()
    {
        //Do something with the class
    }
}

您可以簽出此代碼以找到您的解決方案,而不必傳遞類實例或鍵入參數等。

class Program
{
    static void Main(string[] args)
    {
        var c = new Class1();
        c.Method1();
    }
}

class Class1
{
    public void Method1()
    {
        var obj = new TestClass();
        obj.TestMethod1();
    }
}

class TestClass
{
    public void TestMethod1()
    {
        TestMethod2();
        var mth = new StackTrace().GetFrame(1).GetMethod();
        var clss = mth.ReflectedType.Name;
        Console.WriteLine("Classname in Method1(): {0}", clss);
    }

    private void TestMethod2()
    {
        //get the calling class 
        var mth = new StackTrace().GetFrame(1).GetMethod();
        var clss = mth.ReflectedType.Name;
        Console.WriteLine("Class in .Method2(): {0}", clss);
    }
}

這將獲得首先調用TestClassType 它打印:

TestStack.Class1
TestStack.Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TestStack
{
    class Class1
    {
        public void Method1()
        {
            var obj = new TestClass();
            obj.TestMethod1();
        }
    }

    class TestClass
    {
        public void TestMethod1()
        {
            TestMethod2();
        }

        private void TestMethod2()
        {
            StackTrace st = new StackTrace();
            Type calling = null;
            foreach (var sf in st.GetFrames())
            {
                var type = sf.GetMethod().DeclaringType;
                if (type != this.GetType())
                {
                    calling = type;
                    break;
                }
            }
            Console.WriteLine(calling);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 class1 = new Class1();
            class1.Method1();
            TestClass testClass = new TestClass();
            testClass.TestMethod1();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM