1

I have a collection which I am exposing to a clearscript engine, at the moment I have to specify that the Value property is a static type int so that scripting basic arithmetic as plus or minus will work:

Here's my collection and the class it makes use of that I expose to the script engine:

public class LogicBlockIOCollection : ConcurrentDictionary<string, LogicBlockIO> { }

public class LogicBlockIO
{
    public string Name;
    public dynamic Value;
}

I am adding host objects to my clearscript egine using the following method:

public void AddBlock(LogicBlockIOCollection Entries)
{
    foreach (var Entry in Entries)
    {
        this[LastBlockID].Engine.Script[Entry.Key] = Entry.Value.Value;
    }
}

When I program the LogicBlockIO.Value property as dynamic and run the following script I receive these results:

C = A + B;
C = 1 + 2;
C = 12;  //Not 3 as expected

When I program the LogicBlockIO.Value property as integer, and I try to run the same simple script i get the results I expect:

   C = 3;

Does someone have a suggestion how it is possible to make use of a single property that may be of different types to achieve what I require?

PigSpider
  • 881
  • 9
  • 18
  • Could you clarify your question a bit? Your two code samples don't seem to have anything in common. What are the types of `Entry` and `Entries`? What type are you "configuring as dynamic", and what exactly does that mean? What exactly are you trying to add together as arithmetic? Can you provide an example of the actual code that's failing to produce expected results? – BitCortex Dec 06 '19 at 14:09
  • Hi there, I have clarified the question with your suggestions. Thanks – PigSpider Dec 09 '19 at 06:51
  • 1
    The JavaScript `+` operator does string concatenation if the operands aren't both numbers. The host has no control over this. In your example it must be that `A` or `B` (or both) is something other than a number. – BitCortex Dec 10 '19 at 14:42

0 Answers0