2015-06-22 112 views
3

我只是想知道什麼是value變量的C#的set訪問的數據類型?C# - 「set」訪問器中「value」的數據類型是什麼?

因爲我想實現的C#set訪問類型提示。

例如,我有一個setter方法:

public User 
{ 

    private string username; 

    public void setUsername(SingleWord username) 
    { 
     this.username = username.getValue(); // getValue() method from "SingleWord" class returns "string" 
    } 

} 

現在我該怎樣在C#的語法訪問實現這個?

public User 
{ 
    public string Username 
    { 
     get ; 
     set { 
      // How do I implement type-hinting here for class "SingleWord"? 
      // Is it supposed to be: 
      // this.Username = ((SingleWord)value).getValue(); ??? 
     } 
    } 

} 

所以,我可以這樣調用它:

User newuser = new User() { 
    Username = new SingleWord("sam023") 
}; 

提前感謝!

編輯:下面是SingleWord源代碼:我用這個類通過添加SingleWord從設定器所需要的數據類型,以提供後端驗證

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using Guitar32.Exceptions; 
using Guitar32.Common; 

namespace Guitar32.Validations 
{ 
    public class SingleWord : Validator, IStringDatatype 
    { 
     public static String expression = "^[\\w\\S]+$"; 
     public static String message = "Spaces are not allowed"; 
     private String value; 

     public SingleWord(String value, bool throwException = false) { 
      this.value = value; 
      if (throwException && value != null) { 
       if (!this.isValid()) { 
        throw new InvalidSingleWordException(); 
       } 
       //if (this.getValue().Length > 0) { 
       // if (!this.isWithinRange()) { 
       //  throw new Guitar32.Exceptions.OutOfRangeLengthException(); 
       // } 
       //} 
      } 
     } 

     public int getMaxLength() { 
      return 99999; 
     } 

     public int getMinLength() { 
      return 1; 
     } 

     public String getValue() { 
      return this.value; 
     } 

     public bool isWithinRange() { 
      return this.getValue().Length >= this.getMinLength() && this.getValue().Length <= this.getMaxLength(); 
     } 

     public override bool isValid() { 
      return this.getValue().Length > 0 ? Regex.IsMatch(this.getValue(), expression) : true; 
     } 
    } 

    public class InvalidSingleWordException : Exception { 
     public InvalidSingleWordException() : base("Value didn't comply to Single Word format") 
     { } 
    } 
} 

+1

你可以嘗試像顯式類型轉換爲這裏找到: https://msdn.microsoft.com/en-us/library/85w54y0a.aspx –

+0

值是關鍵字,其設置變量的預測值通過屬性到相同類型的指定值 –

+0

@BabuJames感謝您的顯式類型轉換的想法。 –

回答

5

類型的value是屬性的類型,不管是什麼。

在你的榜樣

所以,

public string Username 
{ 
    ... 
    set 
    { 
     value.GetType() // -> string 
     ... 
    } 
} 

了簡單的解決方案,你要找的是剛剛打電話.getValue()SingleWord例如,

User newuser = new User() 
{ 
    Username = new SingleWord("sam023").getValue() 
}; 

或者更好的,但我相信什麼這將不起作用,因爲你沒有給我們的代碼,

User newuser = new User() 
{ 
    Username = "sam023" 
}; 

但是,如果這是一個絕對不行,你要找的是implicit operatorSingleWord。如果你有能力修改這個類,你可以添加一個如下所示的操作符,它會自動執行轉換爲string,這樣你就可以使用你列出的語法。

public static implicit operator string(SingleWord d) 
{ 
    return d.getValue(); 
} 
+0

謝謝! 無論如何,我添加了我的'SingleWord'類的源代碼。 我實際上使用該類作爲我的後端驗證。 –