在C#中获取属性名称

时间:2012-05-11 13:51:37

标签: c#

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

目前,webservice返回所有字符串,我想将这些字符串转换/验证为更有用的类型。问题是我当前正在通过方法调用中的字符串参数传递属性名称。有没有办法获取一个属性的名称,以便在错误消息中显示而不将其作为字符串传递?

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;
    }
}

3 个答案:

答案 0 :(得分:1)

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

Workaround for lack of 'nameof' operator in C# for type-safe databinding?

答案 1 :(得分:0)

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

在当前/旧版本中,您必须使用Oblivion2000发布的技巧

答案 2 :(得分:0)

这是CℓintonSheppard的帖子: 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; }
}