public bool TryCast()

in src/Avalonia.Base/Rendering/Composition/Expressions/ExpressionVariant.cs [666:821]


        public bool TryCast<T>(out T res) where T : struct
        {
            if (typeof(T) == typeof(bool))
            {
                if (Type == VariantType.Boolean)
                {
                    res = (T) (object) Boolean;
                    return true;
                }
            }

            if (typeof(T) == typeof(float))
            {
                if (Type == VariantType.Scalar)
                {
                    res = (T) (object) Scalar;
                    return true;
                }
                if (Type == VariantType.Double)
                {
                    res = (T)(object)Scalar;
                    return true;
                }
            }
            
            if (typeof(T) == typeof(double))
            {
                if (Type == VariantType.Double)
                {
                    res = (T) (object) Double;
                    return true;
                }

                if (Type == VariantType.Scalar)
                {
                    res = (T)(object)(float)Double;
                    return true;
                }
            }

            if (typeof(T) == typeof(Vector2))
            {
                if (Type == VariantType.Vector2)
                {
                    res = (T) (object) Vector2;
                    return true;
                }

                if (Type == VariantType.Vector)
                {
                    res = (T) (object) Vector.ToVector2();
                    return true;
                }
            }
            
            if (typeof(T) == typeof(Vector))
            {
                if (Type == VariantType.Vector)
                {
                    res = (T) (object) Vector;
                    return true;
                }

                if (Type == VariantType.Vector2)
                {
                    res = (T)(object)new Vector(Vector2);
                    return true;
                }
            }

            if (typeof(T) == typeof(Vector3))
            {
                if (Type == VariantType.Vector3)
                {
                    res = (T) (object) Vector3;
                    return true;
                }
                if (Type == VariantType.Vector3D)
                {
                    res = (T) (object) Vector3D.ToVector3();
                    return true;
                }
            }
            
            if (typeof(T) == typeof(Vector3D))
            {
                if (Type == VariantType.Vector3D)
                {
                    res = (T) (object) Vector3D;
                    return true;
                }
                
                if (Type == VariantType.Vector3)
                {
                    res = (T)(object)new Vector3D(Vector3);
                    return true;
                }
            }

            if (typeof(T) == typeof(Vector4))
            {
                if (Type == VariantType.Vector4)
                {
                    res = (T) (object) Vector4;
                    return true;
                }
            }

            if (typeof(T) == typeof(Matrix3x2))
            {
                if (Type == VariantType.Matrix3x2)
                {
                    res = (T) (object) Matrix3x2;
                    return true;
                }
            }
            
            if (typeof(T) == typeof(Matrix))
            {
                if (Type == VariantType.AvaloniaMatrix)
                {
                    res = (T) (object) Matrix3x2;
                    return true;
                }
            }

            if (typeof(T) == typeof(Matrix4x4))
            {
                if (Type == VariantType.Matrix4x4)
                {
                    res = (T) (object) Matrix4x4;
                    return true;
                }
            }

            if (typeof(T) == typeof(Quaternion))
            {
                if (Type == VariantType.Quaternion)
                {
                    res = (T) (object) Quaternion;
                    return true;
                }
            }
            
            if (typeof(T) == typeof(Avalonia.Media.Color))
            {
                if (Type == VariantType.Color)
                {
                    res = (T) (object) Color;
                    return true;
                }
            }

            res = default;
            return false;
        }