繁体   English   中英

在C#中获取属性名称

[英]Getting Property Name in C#

我有一个验证类,在这个类中,我想验证从Web服务接收到的各种属性是否有效,如果无效,则报告一个描述性错误消息。

当前,Web服务返回所有字符串,我想将它们转换/验证为更有用的类型。 问题是我当前正在通过方法调用中的字符串参数传递属性名称。 有没有一种方法可以获取要显示在错误消息中的属性名称,而无需将其作为字符串传递呢?

public class WebserviceAccess
{
    public MyUsefulDataObject ConvertToUsefulDataObject(WebserviceResponse webserviceResponse)
    {
        var usefulData = new MyUsefulDataObject();

        usefulData.LastUpdated = webserviceResponse.LastUpdated.IsValidDateTime("LastUpdated");
        // etc . . .
        // But I don't want to have to pass "LastUpdated" through.
        // I'd like IsValidDateTime to work out the name of the property when required (in the error message).

        return usefulData ;
    }
}

public static class WebServiceValidator
{
    public static DateTime IsValidDateTime(this string propertyValue, string propertyName)
    {
        DateTime convertedDate;

        if (!DateTime.TryParse(propertyValue, out convertedDate))
        {
            throw new InvalidDataException(string.Format("Webservice property '{0}' value of '{1}' could not be converted to a DateTime.", propertyName, propertyValue));
        }

        return convertedDate;
    }
}

非常感谢您的协助。

提前致谢。

编辑:使用Oblivion2000的建议,我现在有以下内容:

public class Nameof<T>
{
    public static string Property<TProp>(Expression<Func<T, TProp>> expression)
    {
        var body = expression.Body as MemberExpression;

        if (body == null)
        {
            throw new ArgumentException("'expression' should be a member expression");
        }

        return body.Member.Name;
    }
}

public class WebserviceAccess
{
    public MyUsefulDataObject ConvertToUsefulDataObject(WebserviceResponse webserviceResponse)
    {
        var usefulData = new MyUsefulDataObject();

        usefulData.LastUpdated = Nameof<WebserviceResponse>.Property(e => e.LastUpdated).IsValidDateTime(webserviceResponse.LastUpdated);
        // etc . . .

        return usefulData ;
    }
}

public static class WebServiceValidator
{
    public static DateTime IsValidDateTime(this string propertyName, string propertyValue)
    {
        DateTime convertedDate;

        if (!DateTime.TryParse(propertyValue, out convertedDate))
        {
            throw new InvalidDataException(string.Format("Webservice property '{0}' value of '{1}' could not be converted to a DateTime.", propertyName, propertyValue));
        }

        return convertedDate;
    }
}

也许此链接可以为您提供获取参数信息的好方法。

解决C#中缺乏用于类型安全数据绑定的'nameof'运算符的解决方法?

在Visual Studio 2011中,有一个新功能可以处理此问题: http : //www.mitchelsellers.com/blogs/2012/02/29/visual-studio-11-caller-member-info-attributes.aspx

在当前/较旧的版本中,您必须使用Oblivion2000之类的技巧

这是Cinton Sheppard的相关文章: http ://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/

将其保存在书签中对我来说非常有用。 就个人而言,我喜欢他的静态嵌套类方式(从上面引用):

public class Sample2
{
    public static class BoundPropertyNames
    {
        public static readonly string Foo = ((MemberExpression)((Expression<Func<Sample2, int>>)(s => s.Foo)).Body).Member.Name;
    }

    public int Foo { get; set; }
}

暂无
暂无

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

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