Hi,
How do I check if a given object is nullable in other words how to implement the following method...
bool IsNullableValueType(object o)
{
...
}
EDIT: I am looking for nullable value types. I didn't have ref types in mind.
//Note: This is just a sample. The code has been simplified
//to fit in a post.
public class BoolContainer
{
bool? myBool = true;
}
var bc = new BoolContainer();
const BindingFlags bindingFlags = BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Instance
;
object obj;
object o = (object)bc;
foreach (var fieldInfo in o.GetType().GetFields(bindingFlags))
{
obj = (object)fieldInfo.GetValue(o);
}
obj now refers to an object of type bool
(System.Boolean
) with value equal to true
. What I really wanted was an object of type Nullable<bool>
So now as a work around I decided to check if o is nullable and create a nullable wrapper around obj.