如何获取 C# 中的任何表达式作为字符串?
在许多情况下,需要表达式、参数、语句等的名称。例如:
public abstract void Log(string methodName, string parameterName, string message);
public void FooMethod(string value)
{
if (value == null)
{
this.Log("FooMethod", "value", "The value must be whatever...");
throw new ArgumentNullException("value");
}
if (value.Length < 5)
{
this.Log("FooMethod", "value.Length", "The value length must be whatever...");
throw new ArgumentException("value");
}
}
是否有任何方法可以自动获取这些字符串文字,例如使用可以像 typeof(string)? 或者是否有一种基于反射的简单且高性能的方法?
我并不是在寻找一种方法来检查和记录此参数(这实际上只是一个示例)。我正在寻找一种方法来获取部分代码作为字符串
。
以下内容会更准确,可以由编译器检查,并且在重构代码时也会考虑:
public void FooMethod(string value)
{
if (value == null)
{
this.Log(literal(this.FooMethod), literal(value), "The parameter '" + literal(value) + "' must be whatever...");
throw new ArgumentNullException(literal(value));
}
if (value.Length < 5)
{
this.Log(literal(this.FooMethod), literal(value.Length), "The value length must be whatever...");
throw new ArgumentException(literal(value));
}
}
In many cases one needs the name of an expression, parameter, statement, etc. For example:
public abstract void Log(string methodName, string parameterName, string message);
public void FooMethod(string value)
{
if (value == null)
{
this.Log("FooMethod", "value", "The value must be whatever...");
throw new ArgumentNullException("value");
}
if (value.Length < 5)
{
this.Log("FooMethod", "value.Length", "The value length must be whatever...");
throw new ArgumentException("value");
}
}
Is there any way of getting these string literals automatically like for example with a keyword that can be used like typeof(string)
?
Or is there a simple and performant approach based on reflection?
I'm not looking for a way to check and log this parameter (which is actually only an example). I'm looking for a method to get part of the code as string
.
The following would be more accurate, could be checked by the compiler and would also be considered when refactoring the code:
public void FooMethod(string value)
{
if (value == null)
{
this.Log(literal(this.FooMethod), literal(value), "The parameter '" + literal(value) + "' must be whatever...");
throw new ArgumentNullException(literal(value));
}
if (value.Length < 5)
{
this.Log(literal(this.FooMethod), literal(value.Length), "The value length must be whatever...");
throw new ArgumentException(literal(value));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为所有可能的类型创建这样的静态方法。下面是方法名称。
You can create static methods like this for all possible types. Below is for method name.
听起来您需要StackTrace.GetFrame。此方法将为您提供所需的 StackFrame 对象,您可以从中找到此信息。
查看以下代码示例
顺便说一下,在第二个代码示例中,Log 方法调用永远不会执行,因为在调用之前会引发异常。抛出异常后,除了
finally
块中的代码之外,方法中的任何代码都不会执行。请参阅本文。Sounds like you need StackTrace.GetFrame. This method will get you the required StackFrame object from which you can find this information.
Check out the following code sample
By the way, in your second code example the Log method calls will never execute because an exception is throw before the calls. No code in a method will execute after an exception is thrown except for code in a
finally
block. See this article.这取决于您的目标是什么。如果您想获取运行时值
然后您可以像这样使用它:
它将检查该值是否为如果值为 null,则将适当的消息格式化为 ArgumentNullException。
不过,如果您使用的是 .NET 4.0,我建议您使用合约来代替。并不是说它实际上解决了您的特定需求,而是与“老式方式”相比,使用合同进行参数检查在文档和静态分析方面有很多好处
It depends on what your goal is. If you're looking to get the runtime values Martin Doms answer might be the solution. If you wish to be able to link say ArgumentNullException to a specific argument name, and ensure that if you change the name you can use refactoring you can do something like this:
you can then use it like so:
it will check if the value is null and format an appropriate message to the ArgumentNullException if value is null.
How ever if you are on .NET 4.0 I'd suggest using contracts for this stuff instead. Not that it actually solves your particular needs here but argument checking using Contracts has a lot of benefits when it comes to documentation and static analysis compared to doing it "the old fashionged way"