Posts: 1,049
Threads: 249
Joined: Jul 2022
05-01-2023, 04:48 PM
(This post was last modified: 05-01-2023, 04:52 PM by Davider.)
I have encountered many similar problems, for example, the C # code below will prompt an error during execution
#1. What is the highest supported version of C#? C#5?
#2. Is there a better way to make the code in QM support C#6? or High? (.NET Framework 4.6 suport C#6.0)
Thanks for any suggestions and help
david
Macro Macro9
_s=
;using System;
;using System.Linq;
;using System.Text.RegularExpressions;
;
;public class Program
;{
;,public static void Main()
;,{
;,,string s = "xzc abc(s \"(abc)\")); dfg()";
;,,string p = @"abc\(((?:(?>[^()]*)|\((?<Open>)|\)(?<-Open>))*(?(Open)(?!)))\)";
;,,string[] result = FindMatch(s, p);
;,,Console.WriteLine("Full match: " + result[0]);
;,,Console.WriteLine("Group matches: " + string.Join(", ", result.Skip(1)));
;,}
;
;,public static string[] FindMatch(string s, string pattern)
;,{
;,,Match match = Regex.Match(s, pattern);
;,,string fullMatch = match.Value;
;
;,,string[] groupMatches = null;
;,,if (match.Groups.Count > 1)
;,,{
;,,,groupMatches = new string[match.Groups.Count - 1];
;,,,for (int i = 1; i < match.Groups.Count; i++)
;,,,{
;,,,,groupMatches[i - 1] = match.Groups[i].Value;
;,,,}
;,,}
;
;,,return groupMatches == null ? new string[] { fullMatch } : new string[] { fullMatch }.Concat(groupMatches).ToArray();
;,}
;}
str s=
;xzc abc(s \"(abc)\")); dfg()
str s2=
;abc\(((?:(?>[^()]*)|\((?<Open>)|\)(?<-Open>))*(?(Open)(?!)))\)
CsScript x.AddCode(_s)
out x.Call("Program.FindMatch" s s2)
Posts: 12,095
Threads: 142
Joined: Dec 2002
C# 5.
QM uses C:\windows\Microsoft.NET\Framework\v4.0.30319\CSC.EXE. It is installed with Windows.
It seems new csc.exe can be installed, but I did not test. Probably would install not in C:\windows\Microsoft.NET\Framework\v4.0.30319.
https://social.msdn.microsoft.com/Forums...arpgeneral
Posts: 1,049
Threads: 249
Joined: Jul 2022
Thanks for your help!
I used chatgpt to change the previous code to C # 5, but still reported the same error,
Using linqpad5, the codes can be executed successfully
Error (RT) in Macro10: 0x80131600,
C#(13,74): error CS1061: 'System.Array' does not contain a definition for 'Skip' and no extension method 'Skip' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
C#(31,81): error CS1061: 'System.Array' does not contain a definition for 'Concat' and no extension method 'Concat' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?).
Macro Macro4
_s=
;using System;
;using System.Linq;
;using System.Text.RegularExpressions;
;
;public class Program
;{
;;;;;public static void Main()
;;;;;{
;;;;;;;;;string s = "xzc abc(s \"(abc)\")); dfg()";
;;;;;;;;;string p = @"abc\(((?:(?>[^()]*)|\((?<Open>)|\)(?<-Open>))*(?(Open)(?!)))\)";
;;;;;;;;;string[] result = FindMatch(s, p);
;;;;;;;;;Console.WriteLine("Full match: {0}", result[0]);
;;;;;;;;;Console.WriteLine("Group matches: {0}", string.Join(", ", result.Skip(1)));
;;;;;}
;
;;;;;public static string[] FindMatch(string s, string pattern)
;;;;;{
;;;;;;;;;Match match = Regex.Match(s, pattern);
;;;;;;;;;string fullMatch = match.Value;
;
;;;;;;;;;string[] groupMatches = null;
;;;;;;;;;if (match.Groups.Count > 1)
;;;;;;;;;{
;;;;;;;;;;;;;groupMatches = new string[match.Groups.Count - 1];
;;;;;;;;;;;;;for (int i = 1; i < match.Groups.Count; i++)
;;;;;;;;;;;;;{
;;;;;;;;;;;;;;;;;groupMatches[i - 1] = match.Groups[i].Value;
;;;;;;;;;;;;;}
;;;;;;;;;}
;
;;;;;;;;;return groupMatches == null ? new[] { fullMatch } : new[] { fullMatch }.Concat(groupMatches).ToArray();
;;;;;}
;}
str s=
;xzc abc(s \"(abc)\")); dfg()
str s2=
;abc\(((?:(?>[^()]*)|\((?<Open>)|\)(?<-Open>))*(?(Open)(?!)))\)
CsScript x.AddCode(_s)
out x.Call("Program.FindMatch" s s2)
Posts: 1,049
Threads: 249
Joined: Jul 2022
05-02-2023, 01:33 AM
(This post was last modified: 05-02-2023, 01:33 AM by Davider.)
I downloaded the csc.exe compiler for C # 6.0 and added the folder(C:\Program Files (x86)\MSBuild\14.0\Bin) to the environment variable Path
But when executing the following code, there is still an error
How modify the csc.exe path used by QM? Is this possible?
Macro Macro5
_s=
;using System;
;
;//C#6.0 Test
;class Program {
;;;;;static void Main() {
;;;;;;;;;var guid = Guid.NewGuid();
;;;;;;;;;Console.WriteLine($"{guid:N}\r\n{guid:D}\r\n{guid:B}\r\n{guid:P}");
;;;;;;;;;Console.ReadKey(true);
;;;;;}
;}
CsScript x.AddCode(_s)
out x.Call("Program.Main")
Posts: 12,095
Threads: 142
Joined: Dec 2002
It seems impossible, but maybe.
google: ICodeCompiler change csc path
Posts: 1,049
Threads: 249
Joined: Jul 2022
05-02-2023, 05:21 PM
(This post was last modified: 05-02-2023, 05:23 PM by Davider.)
Replace the new version of CSC.exe and its related files to C:\windows\Microsoft.NET\Framework\v4.0.30319
QM can execute C#6 C#7 code now, but the compilation speed is slow, around 3 seconds
Posts: 12,095
Threads: 142
Joined: Dec 2002
Yes, it should be that slow, because uses Roslyn, which is a large and slow .NET library, and .NET code is JIT-compiled before can run. Each compilation executes new csc process. Cannot make faster.
LibreAutomate uses Roslyn too, therefore its startup and the first compilation are slow. But next compilations are fast. Also it uses good caching, therefore don't need to compile assemblies if the source code etc did not change. QM uses only in-memory caching.
Posts: 1,049
Threads: 249
Joined: Jul 2022
05-06-2023, 01:15 AM
(This post was last modified: 05-06-2023, 01:28 AM by Davider.)
thanks! Such is the case
I have encountered another problem:
Using the system's built-in csc.exe compiler:
macro cs5 is successful for Static function outStr ( This method is very convenient for calling C # static methods)
macro cs6 is failed for Static function genGuid
So, I use a higher version of the csc.exe compiler to export the code of cs6 to COM component DLL(And already registered RegisterNetComComponent "$desktop$\cs6.dll" 2|4),
How to call the static methods in COM components in QM?
Macro cs5
_s=
;using System;
;
;//C#5 Test
;public class cla
;{
;,public static void outStr(string s)
;,{
;,Console.Write(s);
;,}
;}
CsScript x.AddCode(_s)
out x.Call("outStr" "hello") ;;OK
Macro cs6
_s=
;using System;
;
;//C#6 Test
;public class cla
;{
;,public static string genGuid()
;,{
;,,var guid = Guid.NewGuid();
;,,string s = $"{guid:N}\r\n{guid:D}\r\n{guid:B}\r\n{guid:P}";
;,,return s;
;,}
;}
CsScript x.AddCode(_s)
out x.Call("genGuid") ;;No
Posts: 12,095
Threads: 142
Joined: Dec 2002
COM methods cannot be static. Probably need to define an interface, and a class that implements it, add ComVisible attribute, define that interface in QM too, in QM create object of that interface type, and call functions through that object. Not tested.
Posts: 12,095
Threads: 142
Joined: Dec 2002
05-06-2023, 04:32 AM
(This post was last modified: 05-06-2023, 04:33 AM by Gintaras.)
Note: QM does not support current .NET versions. Supports .NET 4.x, but not 5, 6, 7, 8...
Posts: 1,049
Threads: 249
Joined: Jul 2022
05-06-2023, 09:13 AM
(This post was last modified: 05-06-2023, 09:16 AM by Davider.)
Thanks for your guidance! I have learned a lot
It's a bit strange, using built-in compiler
Macro Macro4
_s=
;using System;
;
;namespace ns1
;{
;,public class cla1
;,{
;,,public static void func(string s)
;,,{
;,,,Console.Write(s);
;,,}
;,}
;}
CsScript x.AddCode(_s)
;x.Call("ns1.cla1.func" "test")
x.Call("cla1.func" "test")
Posts: 1,338
Threads: 61
Joined: Jul 2006
05-06-2023, 12:42 PM
(This post was last modified: 05-06-2023, 12:45 PM by Kevin.)
from
CsScript.Call help
function` $name [`a1] [`a2] [`a3] [`a4] [`a5] [`a6] [`a7] [`a8] [`a9] [`a10] <tip "PARAMETERS">?
Calls a public static function (method or property).
Returns its return value.
name - function name.
Can be full name, like "Class1.Func1", "Namespace1.Class1.Func1", "VbModule.Func1", or just function name.
If function name is "*", calls the first found public static function.
If function name is "", calls the entry function (usually Main; see <open>CsScript.Exec).
To set or get a property, add set_ or get_ prefix to the property name.
a1-a10 - arguments.
Remarks
Use <help>CsScript.AddCode to add code that contains the function.
The class or VB module must be public too.
Automatically converts argument and return types, if possible. For example, int to bool, int to uint, int to string, string to DateTime, DATE to DateTime. To pass by reference, instead <help "CsScript example - create object">create object.
Supports overloads that have different number of parameters. If number of parameters is same, calls the first overload and converts argument types if need.
See also: <help>CsFunc, <help>VbFunc
Errors: <fa "4504362 /5575008 ERR_INIT">ERR_INIT, <fa "4504362 /5575008 >errors">>errors. <help #IDP_ERR>?
Class help: <tip>CsScript help
so on second call
syntax should be
_s=
;using System;
;
;namespace ns1
;{
;,public class cla1
;,{
;,,public static void func(string s)
;,,{
;,,,Console.Write(s);
;,,}
;,}
;}
CsScript x.AddCode(_s)
;x.Call("ns1.cla1.func" "test")
x.Call("func" "test")
Posts: 1,049
Threads: 249
Joined: Jul 2022
05-06-2023, 04:39 PM
(This post was last modified: 05-06-2023, 04:42 PM by Davider.)
Thanks for your help
Deleting namespaces can be executed successfully
In some code, including namespaces or class can also be executed successfully,
like: " Namespace1.Class1.Func1", " Class1.Func1", " Func1"
Macro Macro3
_s=
;using System;
;
;
;,public class cla1
;,{
;,,public static void func(string s)
;,,{
;,,,Console.Write(s);
;,,}
;,}
CsScript x.AddCode(_s)
;x.Call("ns1.cla1.func" "test")
x.Call("cla1.func" "test")
Posts: 1,049
Threads: 249
Joined: Jul 2022
05-08-2023, 01:37 AM
(This post was last modified: 05-08-2023, 01:45 AM by Davider.)
The following C # code failed to execute
I searched for a lot of information but couldn't find the problem
The code below is C# 4.0 version, Using linqpad5 can execute successfully
Cannot include extension methods? (.Concat .Skip)
Macro M1
_s=
;using System;
;using System.Linq;
;using System.Collections.Generic;
;using System.Text.RegularExpressions;
;
;public class cs
;{
;,public static void Main()
;,{
;,,string s = "xzc abc(s \"(abc)\")); dfg()";
;,,string p = @"abc\(((?:(?>[^()]*)|\((?<Open>)|\)(?<-Open>))*(?(Open)(?!)))\)";
;,,string[] result = matches(s, p);
;,,Console.WriteLine("Full match: {0}", result[0]);
;,,Console.WriteLine("Group matches: {0}", string.Join(", ", result.Skip(1)));
;,}
;,
;,public static string[] matches(string s, string pattern)
;,{
;,,Match match = Regex.Match(s, pattern);
;,,string fullMatch = match.Value;
;
;,,string[] groupMatches = null;
;,,if (match.Groups.Count > 1)
;,,{
;,,,groupMatches = new string[match.Groups.Count - 1];
;,,,for (int i = 1; i < match.Groups.Count; i++)
;,,,{
;,,,,groupMatches[i - 1] = match.Groups[i].Value;
;,,,}
;,,}
;,,return groupMatches == null ? new string[] { fullMatch } : new string[] { fullMatch }.Concat(groupMatches).ToArray();
;,}
;}
CsScript x.AddCode(_s)
x.Call("Main")
Posts: 12,095
Threads: 142
Joined: Dec 2002
Macro Macro3174
CsScript x.SetOptions("references=System.Core")
x.AddCode(_s)
x.Call("Main")
https://learn.microsoft.com/en-us/dotnet...mework-4.8
Posts: 1,049
Threads: 249
Joined: Jul 2022
|