2013-06-28 91 views
6

我寫了下面的方法與follwing要求 -返回可空類型通用的方法值

  1. 輸入的XMLNode和的attributeName
  2. 回報,一旦發現與傳遞
  3. 相關的屬性名稱值
  4. 如果在傳遞的attributeName中沒有值,則應返回 -

    3.1。對於int -1 3.2。對於日期時間DateTime.MinValue 3.3。對於字符串,null 3.4。對於布爾,null

下面的方法在案例3.4中失敗。

public T AttributeValue<T>(XmlNode node, string attributeName) 
     { 
      var value = new object(); 

      if (node.Attributes[attributeName] != null && !string.IsNullOrEmpty(node.Attributes[attributeName].Value)) 
      { 
       value = node.Attributes[attributeName].Value; 
      } 
      else 
      { 

       if (typeof(T) == typeof(int)) 
        value = -1; 
       else if (typeof(T) == typeof(DateTime)) 
        value = DateTime.MinValue; 
       else if (typeof(T) == typeof(string)) 
        value = null; 
       else if (typeof(T) == typeof(bool)) 
        value = null; 



      } 
      return (T)Convert.ChangeType(value, typeof(T)); 
     } 

當改變這

public System.Nullable<T> AttributeValue<T>(XmlNode node, string attributeName) where T : struct 
     { 
      var value = new object(); 

      if (node.Attributes[attributeName] != null && !string.IsNullOrEmpty(node.Attributes[attributeName].Value)) 
      { 
       value = node.Attributes[attributeName].Value; 
      } 
      else 
      { 

       if (typeof(T) == typeof(int)) 
        value = -1; 
       else if (typeof(T) == typeof(DateTime)) 
        value = DateTime.MinValue; 
       else if (typeof(T) == typeof(string)) 
        return null; 
       else if (typeof(T) == typeof(bool)) 
        return null; 



      } 
      return (T?)Convert.ChangeType(value, typeof(T)); 
     } 

它無法爲字符串類型,即3.3的情況下

期待一些幫助。

+0

你如何_call_在你的第一套代碼中的方法?您需要將它稱爲'AttributeValue (...)'如果您只是調用'AttributeValue (...)',那麼'null'不是'bool'的有效值。編輯:而你的第二種情況失敗,因爲'字符串'不能用於'System.Nullable '因爲'字符串'不是一個值類型的結構。 –

回答

4

感謝了大量的答覆,這是我寫的,它爲我的作品..

它返回null類型。

public T AttributeValue<T>(XmlNode node, string attributeName) 
     { 
      object value = null; 

      if (node.Attributes[attributeName] != null && !string.IsNullOrEmpty(node.Attributes[attributeName].Value)) 
       value = node.Attributes[attributeName].Value; 

      if (typeof(T) == typeof(bool?) && value != null) 
       value = (string.Compare(value.ToString(), "1", true) == 0).ToString(); 

      var t = typeof(T); 
      t = Nullable.GetUnderlyingType(t) ?? t; 

      return (value == null) ? 
       default(T) : (T)Convert.ChangeType(value, t); 
     } 

我這樣稱呼它

const string auditData = "<mydata><data><equipmentStatiticsData><userStatistics maxUsers='100' totalUsers='1' authUsers='0' pendingUsers='' adminAddedUsers='' xmlUsers='' internalDBUsers='' webUsers='' macUsers='' vpnUsers='' xUsers8021=''></userStatistics><equipmentStatistics cpuUseNow='14' cpuUse5Sec='1' cpuUse10Sec='1' cpuUse20Sec='1' ramTotal='31301632' ramUtilization ='1896448' ramBuffer='774144' ramCached='8269824' permStorageUse='24' tempStorageUse='24'></equipmentStatistics><authStatus status='1'></authStatus></equipmentStatiticsData></data></mydata>"; 
    xmlDoc.LoadXml(auditData); 
    var userStatsNode = xmlDoc.SelectSingleNode("/mydata/data/equipmentStatiticsData/userStatistics"); 


    var intNullable = AttributeValue<int?>(userStatsNode, "vpnUsers"); 
    var nullableBoolTrue = AttributeValue<bool?>(userStatsNode, "totalUsers"); 
    var nullableBoolFalse = AttributeValue<bool?>(userStatsNode, "authUsers"); 
    var nullableString = AttributeValue<string>(userStatsNode, "authUsers"); 
    var pendingUsersBoolNull = AttributeValue<bool?>(userStatsNode, "pendingUsers"); 
    var testAttribNullableNotFoundDateTime = AttributeValue<DateTime?>(userStatsNode, "testAttrib"); 
    var testAttrib1NullString = AttributeValue<string>(userStatsNode, "testAttrib"); 
    var maxUsersNullInt = AttributeValue<int?>(userStatsNode, "maxUsers"); 

它很適合我。感謝人們...

+0

我很好,現在返回空值。這看起來更好的解決方案, – Ashish

5

對於3.4,您需要使用bool?作爲T的類型,因此您可以返回null。

然後,您可以使用default關鍵字3.3和3.4(字符串和布爾?)。根據msdn,它將返回null作爲引用類型和值類型的默認值(如int或bool)。

您可以使用它像

return default(T); 
+0

對'bool'情況(3.4)的要求是返回null。這將返回'false'而不會與未找到匹配屬性的情況以及提供「false」的情況區分開來。 –

+0

你不需要用'bool?'作爲泛型參數來使用它,在這種情況下返回null?我的意思是,如果T是'bool',那麼你不能返回false,所以你需要使用'bool?'... –

+0

只是爲了澄清,當然,'bool?'的默認值(T)將會返回NULL –

0

你需要使用bool?bool因爲null不是不可爲空bool有效值打電話給你的第一個代碼集。因爲你不能使用string爲通用型的Nullable<T>,因爲它需要一個價值型structstring是引用類型類

你的第二個代碼塊失敗。

你需要改變你的第一個方法塊找typeof(bool?),並用可空​​布爾類型稱之爲:

public T AttributeValue<T>(XmlNode node, string attributeName) 
{ 
    var value = new object(); 

    if (node.Attributes[attributeName] != null && !string.IsNullOrEmpty(node.Attributes[attributeName].Value)) 
    { 
     value = node.Attributes[attributeName].Value; 
    } 
    else 
    { 
     if (typeof(T) == typeof(int)) 
      value = -1; 
     else if (typeof(T) == typeof(DateTime)) 
      value = DateTime.MinValue; 
     else if (typeof(T) == typeof(string)) 
      value = null; 
     else if (typeof(T) == typeof(bool?)) 
      value = null; 
    } 

    var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); 
    return (T)Convert.ChangeType(value, type); 
} 

然後稱其爲:

bool? value = AttributeValue<bool?>(node, "myAttributeName"); 

您還需要因爲Convert.ChangeType不適用於可爲空的類型。從here快速修復可以解決此問題。(它被包含在上面的代碼中)

編輯:這是你的方法的改進/清潔版本:

public T AttributeValue<T>(XmlNode node, string attributeName) 
{ 
    if (node.Attributes[attributeName] != null && !string.IsNullOrEmpty(node.Attributes[attributeName].Value)) 
    { 
     var value = node.Attributes[attributeName].Value; 
     var type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); 
     return (T)Convert.ChangeType(value, type); 
    } 
    else 
    { 
     if (typeof(T) == typeof(int)) 
      return (T)(object)(-1); 

     return default(T); 
    } 
} 

可以爲不存在的節點添加額外的特殊情況,但所有的情況下,除了int已經是類型的默認值,所以只需使用default(T)來代替。