问题描述
public class User
{
public string Name { get; set; }
public string Family { get; set; }
public string Type { get; set; }
}
还有 Program.cs
文件:
public static void Main()
{
try
{
List<User> users = new List<User>();
users.Add(new User
{
Family = "Zamani",Name = "Farhad",Type = "ADY"
});
DoSomething(users);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
public static void DoSomething(List<User> users)
{
var adult = users.Where(a => // line 36
a.Name.toupperInvariant() == "Farhad".toupperInvariant()
&& a.Family.toupperInvariant() == "zam".toupperInvariant()
).FirstOrDefault();
(string name,string type) = getpassengerInfo(GetType(adult.Type),"farhad");//line 41
}
private static (string name,string type) getpassengerInfo(string v,string d)
{
return (v,d);
}
static string GetType(string type)
{
string result = string.Empty;
switch (type)
{
case "ADT":
result = "Adult";
break;
}
return result;
}
当我在调试模式下运行程序时,异常显示 Stacktrace
的 Line:41
没问题。
但是当我以 Release 模式运行项目时,我在 Stacktrace
中得到了不同的代码行。
在发布模式下显示Line:36
。
为什么?
解决方法
为什么?
因为在发布模式下,优化器可以内联函数并执行其他更改源代码中实际行号的操作。
仅输出堆栈跟踪可能没有帮助(恕我直言)。你得到了什么类型的异常?错误信息是什么?它起源于什么方法?根据我的经验,这些对诊断问题更有帮助。我会将您的异常处理更改为至少 Console.WriteLine(ex)
。这将为您提供所有数据加上堆栈跟踪。
此 link 可能会有所帮助。