简体   繁体   English

C#在程序集中获取项目名称

[英]c# getting the project name within an assembly

I need to find a way to get the class file name that an exception will be thrown in. for example the project's tree structure would be 我需要找到一种方法来获取将引发异常的类文件名。例如,项目的树形结构将是

SchoolCourses (assembly name)
->GraduatePrograms.cs
->UndergraduatePrograms.cs
->CommunityPrograms.cs 
->SchoolCourseMain.cs 

I have tried System.Reflection: 我已经尝试了System.Reflection:

return "ERROR:\n An error has occurred in " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

output: 
    ERROR:
    An error has occurred in SchoolCourses

However, that is returning the project name, I would like it to be more along the lines of 但是,那是返回项目名称,我希望它更像是

output: 
    ERROR:
    An error has occurred in SchoolCourses.GraduatePrograms

您可以只使用this.GetType().Name

return "ERROR:\n An error has occurred in " + this.GetType().Name;

An alternative might be to use Environment.StackTrace property. 一种替代方法是使用Environment.StackTrace属性。

http://msdn.microsoft.com/en-us/library/system.environment.stacktrace%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/system.environment.stacktrace%28v=vs.110%29.aspx

The StackTrace property formats the stack trace information for each method call as follows: StackTrace属性设置每个方法调用的堆栈跟踪信息的格式,如下所示:

"at FullClassName.MethodName(MethodParams) in FileName :line LineNumber " “在FileName的FullClassName.MethodName(MethodParams)处:行LineNumber”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM