簡體   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