Z
zequion1
Guest
I have a function that has the goal of returning the default value of any variable. Therefore, a dynamic parameter receives the input variable. I have reproduced here in the simplest way the problems found.
Problem 1: You cannot return the Default of a nullable variable. Apparently, when a nullable variable is passed to a function that receives it with a dynamic parameter, boxing occurs and the nullable property is lost. Ridiculous?
dynamic MyOrigen;
bool? MyVariable = false;
MyOrigen = MyVariable;
System.Type MyType = MyOrigen.GetType();
System.Type MyType_Nullable_GetUnderlyingType = System.Nullable.GetUnderlyingType(MyType);
bool MyType_EsNullable = (MyType_Nullable_GetUnderlyingType != null); // FAILS. Always returns false.
Problem 2: Trying to instantiate a string fails.
string MyVariable = "AA";
System.Type MyType = MyVariable.GetType();
dynamic MyInstancia = System.Activator.CreateInstance(MyType); // ERROR. {"There is no parameterless constructor defined for this object."} -2146233069
Problem 3: Error when trying to retrieve the GetType of a variable that has a null value.
How can I know if a dynamic is null or contains a variable that has a null value?
If the dynamic contains a variable, I want to know its type even if it has a null value.
dynamic MyOrigen;
string MyVariable = null;
MyOrigen = MyVariable;
System.Type MyType = MyOrigen.GetType(); // ERROR. Unable to bind at runtime on a NULL reference..
Problem 4: (Same as 3 but more simple). Error when trying to retrieve the GetType of a variable that has a null value.
string MyVariable = null;
System.Type MyType = MyVariable.GetType(); // ERROR. Object reference not set to an instance of an object.
Continue reading...
Problem 1: You cannot return the Default of a nullable variable. Apparently, when a nullable variable is passed to a function that receives it with a dynamic parameter, boxing occurs and the nullable property is lost. Ridiculous?
dynamic MyOrigen;
bool? MyVariable = false;
MyOrigen = MyVariable;
System.Type MyType = MyOrigen.GetType();
System.Type MyType_Nullable_GetUnderlyingType = System.Nullable.GetUnderlyingType(MyType);
bool MyType_EsNullable = (MyType_Nullable_GetUnderlyingType != null); // FAILS. Always returns false.
Problem 2: Trying to instantiate a string fails.
string MyVariable = "AA";
System.Type MyType = MyVariable.GetType();
dynamic MyInstancia = System.Activator.CreateInstance(MyType); // ERROR. {"There is no parameterless constructor defined for this object."} -2146233069
Problem 3: Error when trying to retrieve the GetType of a variable that has a null value.
How can I know if a dynamic is null or contains a variable that has a null value?
If the dynamic contains a variable, I want to know its type even if it has a null value.
dynamic MyOrigen;
string MyVariable = null;
MyOrigen = MyVariable;
System.Type MyType = MyOrigen.GetType(); // ERROR. Unable to bind at runtime on a NULL reference..
Problem 4: (Same as 3 but more simple). Error when trying to retrieve the GetType of a variable that has a null value.
string MyVariable = null;
System.Type MyType = MyVariable.GetType(); // ERROR. Object reference not set to an instance of an object.
Continue reading...