Invalid conversion from type HostIndexedProperty to type Boolean #719
-
|
Hello ClearScript Team, The update to ClearScript 7.5.1 resolved several issues regarding its integration into our application. 👍 However, I have now discovered a new problem 😟 There are properties defined as Booleans. When evaluating them via the script engine, I receive the following error message: The evaluation of Is there a switch or setting that allows me to change this behavior? Or is the side effectreferring to related to the best regards |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
Hi @DanielGerlach2, We've tested the following with ClearScript 7.5.0 and 7.5.1. The behavior is identical and correct, as far as we can tell: Imports System
Imports Microsoft.ClearScript.Windows
Public Class App
Public Sub New(adminMode As Boolean)
Me.AdminMode = adminMode
End Sub
Public ReadOnly Property AdminMode() As Boolean
End Class
Module Program
Sub Main(args As String())
Using engine As New VBScriptEngine
engine.DisableDynamicBinding = True
engine.AcceptEnumAsUnderlyingType = True
engine.MarshalEnumAsUnderlyingType = True
engine.AddHostObject("App", new App(True))
Console.WriteLine(engine.Evaluate("App.AdminMode"))
End Using
End Sub
End ModuleThis app prints "True" with both ClearScript versions – 7.5.0 without the enum marshaling flags, and 7.5.1 with and without them. Are you doing something differently? Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
I attempted to reproduce the issue in a small test project, but so far I have not been successful. 😞 The execution flow in our application is somewhat complex; however, I will do my best to outline the scenario below and provide a simplified code example at the end. With your deeper insight into the internal processing, you may be able to identify or reproduce the behavior. The statement to be evaluated is returned by the scriptengine using Evaluate, producing the following results:
The problem also seems to lie at the object level; Evaluating the properties of a subclass appears to be the sticking point here, as direct access to properties of the App works as expected: 😲
If, however, the expression to be evaluated contains additional logic conditions, the evaluation works correctly—for example:
The originally reported error Invalid conversion from type HostIndexedProperty to type Boolean resulted from subsequent instructions attempting to output the returned value as a Boolean. Imports Microsoft.ClearScript
Imports Microsoft.ClearScript.Windows
Module Module1
Sub Main()
Dim pApp As New App(True)
Dim strCode As String
strCode = "App.Session.IsAdminMember"
Console.WriteLine(pApp.Eval(strCode))
strCode = "App.Session.IsAdminMember = True"
Console.WriteLine(pApp.Eval(strCode))
End Sub
End Module
Public Class App
Public Sub New(adminMode As Boolean)
Me.AdminMode = adminMode
Me.Session = New Session(adminMode)
End Sub
Public ReadOnly Property AdminMode() As Boolean
Public Property Session As Session
Public Function Eval(Expression As String) As Object
Dim pFlags As WindowsScriptEngineFlags = WindowsScriptEngineFlags.MarshalDateTimeAsDate
Dim pHIFlags As HostItemFlags
pHIFlags += HostItemFlags.GlobalMembers
Using engine As New VBScriptEngine(pFlags)
engine.DisableDynamicBinding = True
engine.AcceptEnumAsUnderlyingType = True
engine.MarshalEnumAsUnderlyingType = True
engine.NullImportValue = DBNull.Value
engine.AddHostObject("App", pHIFlags, Me)
Return engine.Evaluate(Expression)
End Using
End Function
End Class
Public Class Session
Public Sub New(IsAdminMember As Boolean)
Me.IsAdminMember = IsAdminMember
End Sub
Public ReadOnly Property IsAdminMember() As Boolean
End ClassDo you have any idea what could be causing this behavior? I had also suspected that the problem stemmed from incorporating objects from classes within other DLLs; however, I was unable to confirm this through further evaluation of objects (all of which belonged to the main project). |
Beta Was this translation helpful? Give feedback.
-
|
After further testing, the issue is not due to the complexity of our code or object level, but rather to a feature of VB.NET. In VB.NET, it is possible to define properties with parameters. see here The behavior can be reproduced using the following code. Imports Microsoft.ClearScript
Imports Microsoft.ClearScript.Windows
Module Module1
Sub Main()
Dim pApp As New App(True)
pApp.Session = New Session(True)
Dim strCode As String
strCode = "App.Session.IsAdminMember"
Console.WriteLine("Code:" & Environment.NewLine & strCode)
Console.WriteLine(pApp.Eval(strCode))
strCode = "App.Session.IsAdminMember = True"
Console.WriteLine()
Console.WriteLine("Code:" & Environment.NewLine & strCode)
Console.WriteLine(pApp.Eval(strCode))
strCode = "App.Session.IsAdminMember AND 1=1"
Console.WriteLine()
Console.WriteLine("Code:" & Environment.NewLine & strCode)
Console.WriteLine(pApp.Eval(strCode))
End Sub
End Module
Public Class App
Public Sub New(adminMode As Boolean)
Me.AdminMode = adminMode
End Sub
Public ReadOnly Property AdminMode() As Boolean
Public Property Session As Session
Public Function Eval(Expression As String) As Object
Dim pFlags As WindowsScriptEngineFlags = WindowsScriptEngineFlags.MarshalDateTimeAsDate
Dim pHIFlags As HostItemFlags
pHIFlags += HostItemFlags.GlobalMembers
Using engine As New VBScriptEngine(pFlags)
engine.DisableDynamicBinding = True
engine.AcceptEnumAsUnderlyingType = True
engine.MarshalEnumAsUnderlyingType = True
engine.NullImportValue = DBNull.Value
engine.AddHostObject("App", pHIFlags, Me)
Return engine.Evaluate(Expression)
End Using
End Function
End Class
Public Class Session
Dim mvarIsAdminMember As Boolean
Dim mvarBuffered As Boolean
Public Sub New(IsAdminMember As Boolean)
Me.IsAdminMember = IsAdminMember
End Sub
Public Property IsAdminMember(Optional buffered As Boolean = False) As Boolean
Get
If buffered Then
Return mvarIsAdminMember
Else
Return mvarIsAdminMember
End If
End Get
Set(value As Boolean)
If buffered Then
mvarIsAdminMember = value
Else
mvarIsAdminMember = value
End If
End Set
End Property
End ClassCan the desired behavior be achieved via a setting in the scriptengine, or is this feature potentially not yet supported in ClearScript? |
Beta Was this translation helpful? Give feedback.
Hi @DanielGerlach2,
Yes, and ClearScript supports such properties. As your example demonstrates, VBScript code has full access to
IsAdminMemberand can use it via its native syntax. However, the specific behavior you're seeing deserves an explanation.When script code invokes a parameterized (or indexed) property without arguments, ClearScript returns a special object – a host indexed property. It does that to accommodate languages like JavaScript that don't support indexed properties and must access them via intermediaries.
A host indexed property is designed to work naturally with VBScript by providing a default property b…