如何在 C # 中安全地将 System.Object 强制转换为‘ bool’?

我正在从一个(非泛型的、异构的)集合中提取一个 bool值。

as操作符只能用于引用类型,因此不可能使用 as尝试安全转换到 bool:

// This does not work: "The as operator must be used with a reference type ('bool' is a value type)"
object rawValue = map.GetValue(key);
bool value = rawValue as bool;

如果某个对象的值不是布尔值,那么是否可以做类似的事情来安全地将该对象转换为值类型,而不会出现 InvalidCastException

102016 次浏览

Like this:

if (rawValue is bool) {
bool value = (bool)rawValue;
//Do something
} else {
//It's not a bool
}

Unlike reference types, there's no fast way to try to cast to a value type without two casts. (Or a catch block, which would be worse)

bool value;
if(rawValue is bool)
value = (bool)rawValue;
else {
// something is not right...

You can cast it to a bool? with the as keyword and check the HasValue property.

There are two options... with slightly surprising performance:

  • Redundant checking:

    if (rawValue is bool)
    {
    bool x = (bool) rawValue;
    ...
    }
    
  • Using a nullable type:

    bool? x = rawValue as bool?;
    if (x != null)
    {
    ... // use x.Value
    }
    

The surprising part is that the performance of the second form is much worse than the first.

In C# 7, you can use pattern matching for this:

if (rawValue is bool value)
{
// Use value here
}

Note that you still end up with value in scope (but not definitely assigned) after the if statement.

You haven't defined what you want to have happen if rawValue is not convertible to bool. Common choices are to return false, null, or throw an exception. There's also the possibility of the string representation of rawValue to be convertible to a bool, such as Yes/No, True/False, 1/0, etc.

I would use bool.TryParse to do the conversion. This will succeed if rawValue is a bool or its string value is "True" or "False".

bool result;
if (!bool.TryParse(rawValue as string, out result))
{
// you need to decide what to do in this case
}

You can also try Convert.ToBoolean(rowValue);

Providing you don't actually need to keep a reference to the rawValue, here's a one-liner using the GetValueOrDefault() method of the Nullable<T> structure:

bool value = (map.GetValue(key) as bool?).GetValueOrDefault();

You can also specify a default value using the method overload GetValueOrDefault(T).

I used this check before doing something with object

if(myCrazyObject.GetType().Equals(typeof(bool)))
{
//do smt with it
}

If the goal is to have true only if the raw object is boolean 'true' then one-liner (rawValue as bool?)?? false will do:

object rawValue=null
(rawValue as bool?)?? false
false
rawValue="some string"
(rawValue as bool?)?? false
false
rawValue=true
(rawValue as bool?)?? false
true
rawValue="true"
(rawValue as bool?)?? false
false
rawValue=false
(rawValue as bool?)?? false
false
rawValue=""
(rawValue as bool?)?? false
false
rawValue=1
(rawValue as bool?)?? false
false
rawValue=new Dictionary<string,string>()
(rawValue as bool?)?? false
false`

With C# 9 you can also simplify to:

if(rawValue is true)
{
//do stuff
}
bool value = (rawValue is null) ? false : (bool)rawValue.value;

if rawValue is null then value will be false, otherwise value will receive the correct boolean value.