2010-08-31 79 views
-1

我有一個字符串值需要轉換爲我的用戶定義的自定義類型。如何做到這一點,請幫助我。如何將字符串類型轉換爲用戶定義的自定義類型

public class ItemMaster 
{ 
    public static ItemMaster loadFromReader(string oReader) 
    { 
     return oReader;//here i am unable to convert into ItemMaster type 
    } 
} 
+4

你能發表一個你想要做的例子嗎? – 2010-08-31 14:39:57

+0

你能給一些代碼片段樣本嗎? – 2010-08-31 14:40:17

+8

你知道,它可能更模糊。 – Marc 2010-08-31 14:40:40

回答

4

根據您的類型有兩種方法可以做到這一點。

第一種是在類型中添加一個構造函數,該參數需要String參數。

public YourCustomType(string data) { 
    // use data to populate the fields of your object 
} 

第二種是添加靜態Parse方法。

public static YourCustomType Parse(string input) { 
    // parse the string into the parameters you need 
    return new YourCustomType(some, parameters); 
} 
+1

好的答案,關於第二個例子的說明 - 現狀是命名這個方法'Parse'。 – Jamiec 2010-08-31 14:49:43

+0

@Jamiec,是的,我知道,但我認爲'FromString'這個名字更具描述性。 – jjnguy 2010-08-31 14:52:48

+0

@Jamie,我改變了方法的名稱。 – jjnguy 2010-08-31 14:53:25

2

上的用戶定義的自定義類型創建Parse方法:

public class MyCustomType 
{ 
    public int A { get; private set; } 
    public int B { get; private set; } 

    public static MyCustomType Parse(string s) 
    { 
     // Manipulate s and construct a new instance of MyCustomType 
     var vals = s.Split(new char[] { '|' }) 
      .Select(i => int.Parse(i)) 
      .ToArray(); 

     if(vals.Length != 2) 
      throw new FormatException("Invalid format."); 

     return new MyCustomType { A = vals[0], B = vals[1] };    
    } 
} 

當然,所提供的例子非常簡單,但它至少不會讓你開始。

+0

你能舉個例子嗎? – Pradeep 2010-08-31 14:43:05

0

對於實際的轉換,我們需要查看類結構。然而這個看起來爲骨架如下:

class MyType 
{ 
    // Implementation ... 

    public MyType ConvertFromString(string value) 
    { 
     // Convert this from the string into your type 
    } 
} 
1

Convert.ChangeType()方法可以幫助你。

string sAge = "23"; 
int iAge = (int)Convert.ChangeType(sAge, typeof(int)); 
string sDate = "01.01.2010"; 
DateTime dDate = (DateTime)Convert.ChangeType(sDate, typeof(DateTime)); 
+0

沒有辦法知道這一點,因爲它是用戶定義的類型。 – 2010-08-31 14:44:30

1

首先,您需要定義一種您的類型在轉換爲字符串時將遵循的格式。 一個簡單的例子是社會安全號碼。你可以很容易地將其描述爲正則表達式。

\d{3}-\d{2}-\d{4} 

之後,你很簡單,需要扭轉過程。約定是爲你的類型定義一個Parse方法和TryParse方法。區別在於TryParse不會拋出異常。

public static SSN Parse(string input) 
public static bool TryParse(string input, out SSN result) 

現在,您按照實際解析輸入字符串的過程可以像您希望的那樣複雜或簡單。通常,您會標記輸入字符串並執行語法驗證。 (EX:一個破折號可以去這裏?)

number 
dash 
number 
dash 
number 

這真的取決於你想要多少工作投入它。以下是如何標記字符串的基本示例。

private static IEnumerable<Token> Tokenize(string input) 
{ 
    var startIndex = 0; 
    var endIndex = 0; 
    while (endIndex < input.Length) 
    {    
     if (char.IsDigit(input[endIndex])) 
     { 
      while (char.IsDigit(input[++endIndex])); 
      var value = input.SubString(startIndex, endIndex - startIndex); 
      yield return new Token(value, TokenType.Number); 
     } 
     else if (input[endIndex] == '-') 
     { 
      yield return new Token("-", TokenType.Dash); 
     } 
     else 
     { 
      yield return new Token(input[endIndex].ToString(), TokenType.Error); 
     } 
     startIndex = ++endIndex; 
    } 
} 
相關問題