CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Lambda function should use dynamic type

Open docfresh opened this issue 4 years ago • 1 comments

I believe I have the correct C# code at the end, although I am not 100% certain. In VB I was able to have the lambda function have a boolean, but in C# it appears it needs dynamic.

Also, note the different syntax for accessing members of Application.Session. In VB it is this

        Public Shared ReadOnly Property Session As <Dynamic> Object

but in C# it is this.

        public static dynamic Session { get; }`

VB.Net input code

    Public Sub DetectIOSWebAppStandalone()
        'does a one-time check of window.navigator.standalone and writes a session flag 
        'so we know if we have been launched in iOS webview 

        Application.Eval("window.navigator.standalone", Sub(a As Boolean)
                                                            If a = True Then
                                                                Application.Session("isIOSstandalone") = True 'its standalone mode
                                                            Else
                                                                Application.Session("isIOSstandalone") = False 'its not standalone mode
                                                            End If
                                                        End Sub)


    End Sub

Erroneous output

        public void DetectIOSWebAppStandalone()
        {
            // does a one-time check of window.navigator.standalone and writes a session flag 
            // so we know if we have been launched in iOS webview (or at least "Add To Homescreen App"
            // need to combine this with hybrid flag... not sure if returns true in hybrid app!

            Application.Eval("window.navigator.standalone", (bool a) => { if (a == true) { Application.Session("isIOSstandalone") = (object)true; } else { Application.Session("isIOSstandalone") = (object)false; } }); // its standalone mode
                                                                                                                                                                                                                         // its not standalone mode
        }

Expected output

        public void DetectIOSWebAppStandalone()
        {
            // does a one-time check of window.navigator.standalone and writes a session flag 
            // so we know if we have been launched in iOS webview (or at least "Add To Homescreen App"
            // need to combine this with hybrid flag... not sure if returns true in hybrid app!

            Application.Eval("window.navigator.standalone", (dynamic a) =>
            {
                if (a == true)
                {
                    Application.Session.isIOSstandalone = true;
                } else
                {
                    Application.Session.isIOSstandalone = false;
                }
            });

            //Application.Eval("window.navigator.standalone", (bool a) => { if (a == true) { Application.Session.isIOSstandalone = true; } else { Application.Session.isIOSstandalone = false; } }); // its standalone mode
                                                                                                                                                                                                                         // its not standalone mode
        }

Details

CodeConverter 8.41

Signature of Application.Eval method in C#

        //
        // Summary:
        //     Executes the JavaScript script on the client and receives the return value (or
        //     null) in the callback method.
        //
        // Parameters:
        //   script:
        //     The script to evaluate.
        //
        //   callback:
        //     Asynchronous callback method that receives the return value.
        public static void Eval(string script, Action<dynamic> callback);

Signature of Application.Eval method in VB

        '
        ' Summary:
        '     Executes the JavaScript script on the client and receives the return value (or
        '     null) in the callback method.
        '
        ' Parameters:
        '   script:
        '     The script to evaluate.
        '
        '   callback:
        '     Asynchronous callback method that receives the return value.
        Public Shared Sub Eval(script As String, <Dynamic({False, True})> callback As System.Action(Of Object))`

docfresh avatar Nov 03 '21 19:11 docfresh

Thanks! I'll have to experiment with this one a bit to understand what's going on I hadn't come across DynamicAttribute before. But it looks completely equivalent to the C# version This is a slightly special case that needs to be checked when tackling #782 and #783

GrahamTheCoder avatar Nov 04 '21 08:11 GrahamTheCoder