【发布时间】:2011-11-02 19:18:54
【问题描述】:
如果我从一个属性的getter中抛出一个异常,是否有可能获得
我在其中调用该属性的 catch 块中的属性——例如使用反射或
读取堆栈跟踪?
例如:
class Animal
{
private string _name;
public string Name {
get { throw new Exception(); }
set { _name = value; }
}
}
在另一个地方,我调用 Name 属性的 getter,我想在 catch 块中获取属性名称:
Animal cat = new Animal();
try{
string catName = cat.Name;
}
catch (Exception e)
{
string propertyName = //Here I should be able to reach "Name"
}
【问题讨论】:
-
不要在属性的 getter 中抛出异常。
-
重点是什么,你想做什么,为什么?
-
我也想知道你为什么认为你需要这个。如果你需要这个,我敢打赌你的设计有问题。
-
我希望它用于错误记录目的!
标签: c# .net reflection exception-handling stack-trace