Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

Commented Unassigned: Exception when creating Dates in V8ScriptEngine [113]

$
0
0
Hello,

I'm trying to create/parse date from a JS evaluate and return them as strings back into c# code as such:

```
using (var engine = new V8ScriptEngine())
{
resultEval = engine.Evaluate(expression);
}
```

where my `expression` is

`new Date()`

and it return an "Microsoft.ClearScript.V8.V8ScriptItem" object with an "UnderlyingSystemType" saying:

'((Microsoft.ClearScript.ScriptItem)resultEval).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException'

Now, I feel like this *should* work, but it's not. Any ideas? I don't think anything went wrong during the build/reference process. Other JS libraries such as Math work flawlessly.

Also, this is only an issue when trying to create a `new` date. Calling `Date()` works, but it only displays today's date.
Comments: Hi again, ​​ >I can't seem to use V8ScriptItem for type-checking because it is protected. Is this intended? I found the workaround of using the GetType().FullName to accomplish what I want ​ Right; ClearScript should provide a better way to determine whether something is a script object. Your technique works but is unsupported. You could type-check against [`DynamicObject`](https://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject(v=vs.110).aspx), but host objects can be derived from that class as well. Unfortunately we don't have a better answer at this time. ​ >I guess my question is, with that intent in mind, are there other scenarios where the ScriptEngine returns a V8ScriptItem that isn't a js Date, that I should take in consideration? ​ Yes; a JavaScript function or expression can return any object it can access or construct. ClearScript converts simple values to their .NET equivalents (strings, bools, numbers, `null`, `undefined`, `void`), but all other JavaScript objects (dates, functions, arrays, object literals, etc.) are returned to the host as dynamic objects. Script code can also return any .NET objects you've exposed or passed to the script engine. .NET provides various means of determining an object's type. You can use reflection, try casting to a special type such as `DynamicObject` or [`IConvertible`](https://msdn.microsoft.com/en-us/library/system.iconvertible(v=vs.110).aspx), use [`Type.GetTypeCode`](https://msdn.microsoft.com/en-us/library/system.type.gettypecode(v=vs.110).aspx), etc. And if you know you're dealing with a JavaScript object, you can use language-specific techniques such as `obj.constructor.name`. Good luck!

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles



Latest Images