如何在给定方案中获取类和方法名称?

问题描述 投票:-2回答:1

我使用在线c#编译器来确定类和方法名称。参见下面给出的代码,我故意生成错误。

预期产出是:

Hello, world!
ExceptionTest

,基本上是从生成异常的地方开始的。

输出,我得到的是

Hello, world!
System.Reflection.RuntimeMethodInfo 

//Rextester.Program.Main是代码的入口点。不要改变它。 //适用于Microsoft(R).NET Framework 4.5的编译器版本4.0.30319.17929

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

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
            //Your code goes here
            Console.WriteLine("Hello, world!");
             var abc = new Xyz();
            abc.ExTest();
            }
            catch(Exception ex)
            {
                Console.WriteLine(new StackTrace().GetFrame(1).GetMethod().DeclaringType.FullName);  

            }
        }
    }

    public class Xyz
    {
        public void ExTest()
        {
            var abc = new Abc();
            abc.ExceptionTest();
        }
    }

    public class Abc
    {
        public void ExceptionTest()
        {
            throw new Exception();

        }
    }
}

请注意,这是在一个在线工具qazxsw poi上编译的。我没有在Visual Studio上运行它。

c# system.reflection
1个回答
1
投票

简单地说,你可以使用Exception http://rextester.com/;

TargetSite
© www.soinside.com 2019 - 2024. All rights reserved.