简体   繁体   English

如何获取传递给方法的参数的名称?

[英]How to get the name of a parameter passed to a method?

I currently have a validation class with a method which validates that a string is not null (and logs the info, but that's not the concern here)我目前有一个验证类,其中包含一个验证字符串不为空的方法(并记录信息,但这不是这里的问题)

protected void ValidateStringNotEmpty(string propertyName, string value)
{
    if (string.IsNullOrWhiteSpace(value))
    {
        ValidationErrors.Add($"Property [{propertyName}] must be filled");
    }
}

which is called using这被称为使用

ValidateStringNotEmpty(nameof(Name), Name);

Is there an easy (and optimized) way to prevent the need to pass the first argument nameof(property) ?是否有一种简单(和优化)的方法来防止需要传递第一个参数nameof(property)

I saw solutions using the stacktrace to trace the call back, but it does not seem to be a great solution...我看到了使用 stacktrace 来跟踪回调的解决方案,但这似乎不是一个很好的解决方案......

EDIT: What I'm really trying to do is see if something exists to do this (removing the need to pass the name of the argument).编辑:我真正想做的是看看是否存在可以执行此操作的内容(无需传递参数名称)。 I know the pragmatic solution is the one I already use, and this is not a problem.我知道务实的解决方案是我已经使用的解决方案,这不是问题。

现在有“CallerArgumentExpression”,它随 dotnet 6 / C# 10 一起提供,并且完全符合您的要求

protected void ValidateStringNotEmpty(string value, [CallerArgumentExpression("value")] string propertyName = "")

What about validating directly inside the property and leveraging [CallerMemberName] ?直接在属性内部验证并利用[CallerMemberName]怎么样?

private string _text;

public string Text
{
    get => _text;
    set
    {
        if (IsValid(value))
        {
            _text = value;
        }
        else
        {
            ValidationErrors.Add("'abcd' was expected.");
        }
    }
}

private bool IsValid<T>(T value, [CallerMemberName] string propertyName = null)
{
    switch (propertyName)
    {
        case nameof(Text):
            return value as string == "abcd";
        default:
            throw new ArgumentOutOfRangeException(nameof(propertyName), propertyName, null);
    }
}

public static class ValidationErrors
{
    public static void Add(string message, [CallerMemberName] string propertyName = null)
    {
        throw new NotImplementedException();
    }
}

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

相关问题 如何获取在传递参数的lambda中使用的方法名称 - How to get the method name used in lambda that was passed a parameter 如何在方法内部获取传递参数的nameof()? - How to get nameof() of the passed parameter inside of the method? 获取作为参数传递的委托的方法名称(紧凑框架) - Get method name of a delegate passed as a parameter (Compact Framework) 如果将其名称作为参数传递,如何在方法中打开表单 - How do I open a form in a method if passed its name as a parameter 如何获取泛型函数的方法名<t>传入方法</t> - How to get Method Name of Generic Func<T> passed into Method 我如何在endrequest方法中的_dopostback()中传递参数? - How do i get the parameter passed in _dopostback() in endrequest method? 如何获取作为 func 传递的匿名方法的参数值? - How can i get the parameter values of an anonymous method passed as a func? Moq:如何获取传递给模拟服务方法的参数 - Moq: How to get to a parameter passed to a method of a mocked service 如何获得通过值传递的方法参数的原始表达式? - How to get original expression for method parameter passed by value? 如何限制方法中传递的参数 - How to restrict passed parameter in method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM