Skip to main content
 首页 » 编程设计

C#之无需强制转换即可将对象转换为 int

2024年02月06日16lovecherry

我有一个方法可以根据给定的列(数据库类)返回一种类型的对象。但是,当我分配对象时,编译器会抛出一个错误,指出它无法将对象类型隐式转换为 int。如何在不转换的情况下转换它?

它看起来更好:

this.Id = datum["Id"]; 

但现在我必须包含一个转换,这会使代码变得不那么干净并且更难编码:

this.Id = (int)datum["Id"]; 

这是我的代码:

public object this[string name] 
    { 
        get 
        { 
            object result; 
 
            if (this.Dictionary.ContainsKey(name)) 
            { 
                if (this.Dictionary[name] is DBNull) 
                { 
                    result = null; 
                } 
                else if (this.Dictionary[name] is byte && Meta.IsBool(this.Table, name)) 
                { 
                    result = (byte)this.Dictionary[name] > 0; 
                } 
                else 
                { 
                    result = this.Dictionary[name]; 
                } 
            } 
            else 
            { 
                result = default(object); 
            } 
 
            return result; 
        } 
        set 
        { 
            if (value is DateTime) 
            { 
                if (Meta.IsDate(this.Table, name)) 
                { 
                    value = ((DateTime)value).ToString("yyyy-MM-dd"); 
                } 
                else if (Meta.IsDateTime(this.Table, name)) 
                { 
                    value = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"); 
                } 
            } 
 
            if (this.Dictionary.ContainsKey(name)) 
            { 
                this.Dictionary[name] = value; 
            } 
            else 
            { 
                this.Dictionary.Add(name, value); 
            } 
        } 
    } 

请您参考如下方法:

您可以将您的索引器签名更改为:

public dynamic this[string name] 

这将使转换在执行时动态化。

但就个人而言,我更喜欢强制转换方法。它清楚地表明这是可能会失败的事情 - 您是在告诉编译器您拥有它无法获得的信息。

顺便说一句,利用 Dictionary<,>.TryGetValue 可以更简单地编写代码。以及用于设置的字典索引器的行为:

public object this[string name] 
{ 
    get 
    { 
        object result; 
        if (Dictionary.TryGetValue(name, out result)) 
        { 
            if (result is DBNull) 
            { 
                result = null; 
            } 
            else if (result is byte && Meta.IsBool(this.Table, name)) 
            { 
                result = (byte) result > 0; 
            } 
        } 
        return result; 
    } 
    set 
    { 
        // TODO: Byte/bool conversions? 
 
        if (value is DateTime) 
        { 
            // Note use of invariant culture here. You almost certainly 
            // want this, given the format you're using. Preferably, 
            // avoid the string conversions entirely, but... 
            DateTime dateTime = (DateTime) value; 
            if (Meta.IsDate(this.Table, name)) 
            { 
                value = dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); 
            } 
            else if (Meta.IsDateTime(this.Table, name)) 
            { 
                value = dateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); 
            } 
        } 
 
        Dictionary[name] = value; 
    } 
}