1. Arcady Abramov
  2. Beta Testing
  3. Friday, 17 January 2020 07:50 AM UTC

Hello, 

Since .NET System assembly has numerous extension functions for string and datetime, would it be possible to use it in Powerscript?

If yes, is there an example of it (even though the assembly is in the system path, it is not going to be in the application directory on deployment)

Thank you

Accepted Answer
Michael Kramer Accepted Answer Pending Moderation
  1. Sunday, 19 January 2020 14:40 PM UTC
  2. Beta Testing
  3. # Permalink

I created small demo to check how exceptions travel from C# into PowerScript.

My C# code compiled to .NET Standard 2.0 library

namespace QuickAssembly
{
  public class Demo
  {
    public void CrashNow()
    { 
      throw new System.Exception("Crash on purpose");
} } }

I generated PowerScript proxy using C# Class Importer with these settings:

  • .NET Framework
  • All naming options as default (nvo_, of_, get_, and set_)
  • [X] Add type identifying prefix ...
  • [X] Append numbers ...
  • [X] Encapsulate a CSharpAssembly
  • [  ] Incorporate try-catch ...

My PowerScript test code:

try // Call .NET Standard library
  nvo_demo demo
  demo = create nvo_demo
  demo.of_CrashNow( )
	
catch (RuntimeError ex)
  string msg
  msg  = "Exception Class:~t" + ClassName(ex)     + "~r~n"
  msg += "Error Number:~t"    + string(ex.Number) + "~r~n"
  msg += "Error Message:~t"   + ex.GetMessage()
  MessageBox("EXCEPTION caught in PowerScript", msg)
finally
  destroy demo
end try

This is screen shot of running my app

Conclusion:

System.Exception in C# code is exposed as OLERuntimeError( error no. = 35 ). The exception in PowerScript includes the exception message available in C#.

HTH /Michael

Comment
There are no comments made yet.
Arcady Abramov Accepted Answer Pending Moderation
  1. Sunday, 19 January 2020 05:25 AM UTC
  2. Beta Testing
  3. # 1

Will that work if I try to catch System.Exception inside PB 2019 code when calling .NET assembly?

And how would System.Exception look like in PB Code? 

Let's say I called a .NET function, which generated System.Execption and this exception was not handled in .NET code. How would I be able to catch it in PB 2019 code and prevent the whole system from crashing? 

Comment
  1. Ken Guo @Appeon
  2. Sunday, 19 January 2020 08:53 AM UTC
Hi Arcady,



Please use CSharpAssembly or CSharpObject to call .Net assembly in PB 2019, in the meantime, please also add the Try Catch statement.

If an exception occurs in the .NET function, the Try Catch in PB will also catch the error message in the .NET function.



Regards,

Ken
  1. Helpful
There are no comments made yet.
Michael Kramer Accepted Answer Pending Moderation
  1. Friday, 17 January 2020 16:16 PM UTC
  2. Beta Testing
  3. # 2

Both COM and the new C# interop require an object instantiated in .NET. You cannot call static functions without an object. In PowerScript each global function is a class with one function defined so when you call global function, you actually instantiate an object, call the function, then destroy the object.

Also, each type passed to the .NET class has restrictions on datatypes. (e.g. C# interop doesn't support enums) in parameters nor function return type. Finally, PowerScript doesn't have the async/await equivalent.

Therefore => I suggest create .NET Standard assembly where you class(es) act as intermediary between your PowerScript client and the .NET world. What goes on inside that class is at free disposal and you can add whatever pre- and post-processing required for your PowerScript app.

EXAMPLE: System.Math has great functions like Log2 and FusedMultiplyAdd. However, they are static so they can't be called directly from PowerScript. However, you could create MySystem assembly and namespace where a Math class has functions like Log2 and FusedMultiplyAdd which relay calls to System.Math.

HTH /Michael

Comment
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Friday, 17 January 2020 12:49 PM UTC
  2. Beta Testing
  3. # 3

PB2019 R2 gives us the possibility to no longer need OLE to call non visual C# assemblies which is awesome since we no longer have to wrap things in an activex and generate the registry entries for COM (too bad visual assemblies don't have that new functionality (yet)):

https://docs.appeon.com/appeon_online_help/pb2019r2/application_techniques/ch20.html

If you walk through those pages, there's a sample example.

I don't think the DLL / EXE would have to be in the same folder as the application, as long as it's in the PATH, but I'm not totally sure of that.

 

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.