Let's say, I have following simplified type:
public class Model
{
public decimal? Result { get; set; }
}
How to express null coalescing operator using CodeDOM to generate C# code, is it possible at all? Now I'm using following workaround:
new CodePropertyReferenceExpression(
new CodePropertyReferenceExpression(modelArgument, "Result"),
"Value"))
Which is equal to model.Result.Value
, but not model.Result ?? 0M
Better workaround
CodeExpression
equalent to model.Result.GetValueOrDefault(0M)
suitable for nullable value types
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodePropertyReferenceExpression(modelArgument, "Result"),
"GetValueOrDefault"),
new [] { new CodePrimitiveExpression(0m) })),