11

我写了以下具有以下要求的方法-

  1. 输入是xmlnode和attributeName
  2. 如果找到并传递了关联的属性名称,则返回该值
  3. 如果传递的 attributeName 中没有值,它应该返回 -

    3.1。对于 int -1 3.2。对于日期时间 DateTime.MinValue 3.3。对于字符串,null 3.4。对于布尔,空

对于案例 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

期待一些帮助。

4

3 回答 3

7

对于 3.4,您需要bool?用作 T 的类型,因此您可以返回 null。

然后您可以使用default3.3 和 3.4 的关键字(字符串和布尔值?)。根据msdn,它将返回null引用类型和值类型(如 int 或 bool)的默认值。

你可以像这样使用它

return default(T);
于 2013-06-28T11:53:17.280 回答
6

感谢您的许多回复,这是我写的,它对我有用..

它为类型返回 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");

这对我来说很有用。谢谢人们...

于 2013-06-28T12:46:11.833 回答
0

您需要使用bool?not调用您的第一个代码集,bool因为null它不是 non-nullable 的有效值bool

您的第二个代码块失败,因为您不能string用于泛型类型,Nullable<T>因为它需要值类型struct并且string是引用类型类。

您需要更改您的第一个方法块以查找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不适用于可为空的类型。从这里快速修复解决了这个问题。(它包含在上面的代码中)

编辑:这是您的方法的改进/清理版本:

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)

于 2013-06-28T11:54:07.573 回答