The following example demonstrates how to use the TypeAs(Expression, Type) method to create a UnaryExpression that represents the reference conversion of a non-nullable integer expression to the nullable integer type.
' Create a UnaryExpression that represents a reference
' conversion of an Integer to an Integer? (a nullable Integer).
Dim typeAsExpression As System.Linq.Expressions.UnaryExpression = _
System.Linq.Expressions.Expression.TypeAs( _
System.Linq.Expressions.Expression.Constant(34, Type.GetType("System.Int32")), _
Type.GetType("System.Nullable`1[System.Int32]"))
Console.WriteLine(typeAsExpression.ToString())
' This code produces the following output:
'
' (34 As Nullable`1)
// Create a UnaryExpression that represents a
// conversion of an int to an int?.
System.Linq.Expressions.UnaryExpression typeAsExpression =
System.Linq.Expressions.Expression.TypeAs(
System.Linq.Expressions.Expression.Constant(34, typeof(int)),
typeof(int?));
Console.WriteLine(typeAsExpression.ToString());
// This code produces the following output:
//
// (34 As Nullable`1)