Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Execute c# functions
#1
Hi,

I need to execute functions in C# code in QM, The following error message is prompted after execution

Error (RT) in <open ":1848: /157">Run_C#:  0x80131600, 
    <macro "Scripting_Link /97 12 2 ''Run_C#''">Run_C#(97,12): error CS1502: The best overloaded method match for 'System.Collections.Generic.List<string>.Contains(string)' has some invalid arguments
    <macro "Scripting_Link /97 35 2 ''Run_C#''">Run_C#(97,35): error CS1503: Argument 1: cannot convert from 'object' to 'string'
    <macro "Scripting_Link /155 9 2 ''Run_C#''">Run_C#(155,9): error CS1502: The best overloaded method match for 'System.Collections.Generic.List<string>.Contains(string)' has some invalid arguments
    <macro "Scripting_Link /155 32 2 ''Run_C#''">Run_C#(155,32): error CS1503: Argument 1: cannot convert from 'object' to 'string'.    <help #IDP_ERR>?


In linqpad(.net 4.8) , C# code can be executed successfully 
The result of C# code execution is as follows:

┃Y_Country┃ Country[]United States[]United Kingdom[]France
---------------
My name is ┃N_Name┃, My country is ┃Y_Country┃, My age is ┃N_Age┃, thank you!


This result, which is finally split, is assigned to variables str B C

It is actually the value of variable B and variable C in C# code, In the qm code, get the value of these two variables directly? is it possible?

Thanks in advance for any advice and help
david


Macro Run_C#
Code:
Copy      Help
_s=
;┋Country, United States, United Kingdom, France
;My name is ┃Name┃, My country is ┃Country┃, My age is ┃Age┃, thank you!

CsScript x
x.AddCode("")
_s=x.Call("Formater.Format" _s)
out _s

str B C
tok _s &B 2 "---------------"
out B.trim
out C.trim

#ret
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class Formater
{
,static void Main(string[] args)
,{
,,if (args.Length == 0)
,,{
,,,return;
,,}
,,Console.WriteLine(Format(args[0]));
,}
,static readonly Regex reLabel = new Regex("┋([^,]*),(\\s*)", RegexOptions.Compiled);
,static readonly Regex reLabel2 = new Regex(",\\s*", RegexOptions.Compiled);
,static readonly Regex reContent = new Regex("┃([^┃]*)┃", RegexOptions.Compiled);
,static readonly Regex reDelims = new Regex("\\r?\\n", RegexOptions.Compiled);
,public static string Format(string input)
,{
,,List<string> B = new List<string>();
,,List<string> C = new List<string>();
,,List<string> CLocal = new List<string>();
,,List<string> keysInContent = new List<string>();//#keys in content
,,Dictionary<string, int> dicLabelCtr = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
,,OrderedDictionary dicLabelLocal4B = new OrderedDictionary();
,,//# label
,,MatchEvaluator sbLabel = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelCtr.ContainsKey(k))
,,,{
,,,,int ctr = ++dicLabelCtr[k];
,,,,return string.Format("┃Y_{0}{1}┃{2}{0}{1}[]", k, ctr, m.Groups[2].Value);
,,,}
,,,else
,,,{
,,,,dicLabelCtr[k] = 1;
,,,,return string.Format("┃Y_{0}┃{1}{0}[]", k, m.Groups[2].Value);
,,,}
,,};
,,//# content
,,MatchEvaluator sbContent = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelLocal4B.Contains(k))
,,,{
,,,,keysInContent.Add(k);
,,,,if (dicLabelCtr.ContainsKey(k))
,,,,{
,,,,,return string.Format("┃Y_{0}{1}┃", k, dicLabelCtr[k] + 1);
,,,,}
,,,,else
,,,,{
,,,,,return string.Format("┃Y_{0}┃", k);
,,,,}
,,,}
,,,else
,,,{
,,,,return string.Format("┃N_{0}┃", k);
,,,}
,,};
,,int stack = 0;
,,foreach (var line in reDelims.Split(input))
,,{
,,,if (stack == 0)
,,,{
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,stack = 1;
,,,,,//# procossing prev section
,,,,,for (int i = 0; i < CLocal.Count; i++)
,,,,,{
,,,,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,,,,}
,,,,,foreach (var key in dicLabelLocal4B.Keys)
,,,,,{
,,,,,,if (!keysInContent.Contains(key))
,,,,,,{
,,,,,,,C.Add((string)dicLabelLocal4B[key]);
,,,,,,}
,,,,,,else
,,,,,,{
,,,,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,,,,}
,,,,,}
,,,,,C.AddRange(CLocal);
,,,,,CLocal.Clear();
,,,,,keysInContent.Clear();
,,,,,dicLabelLocal4B.Clear();
,,,,,//# next section
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,,else
,,,{
,,,,//#stack=1
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,stack = 0;
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,}
,,//# procossing prev section
,,for (int i = 0; i < CLocal.Count; i++)
,,{
,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,}
,,foreach (var key in dicLabelLocal4B.Keys)
,,{
,,,if (!keysInContent.Contains(key))
,,,{
,,,,C.Add((string)dicLabelLocal4B[key]);
,,,}
,,,else
,,,{
,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,}
,,}
,,C.AddRange(CLocal);
,,CLocal.Clear();
,,keysInContent.Clear();
,,dicLabelLocal4B.Clear();
,,//return result
,,B.Add("---------------");
,,//Merge variables B and C
,,B.AddRange(C); 
,,return string.Join(Environment.NewLine, B.ToArray());
,}
}
#2
QM uses an old version of C#. Probably it does not support this. Try to cast to string.

if (!keysInContent.Contains((string)key))
#3
To return multiple values from C#, can be used 2 ways:

1. Let C# return array.

Macro Macro3241
Code:
Copy      Help
_s=
;┋Country, United States, United Kingdom, France
;My name is ┃Name┃, My country is ┃Country┃, My age is ┃Age┃, thank you!

CsScript x
x.AddCode("")
ARRAY(str) r=x.Call("Formater.Format" _s)
out "B:"
out r[0]
out "C:"
out r[1]

#ret
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class Formater
{
,static void Main(string[] args)
,{
,,if (args.Length == 0)
,,{
,,,return;
,,}
,,Console.WriteLine(Format(args[0]));
,}
,static readonly Regex reLabel = new Regex("┋([^,]*),(\\s*)", RegexOptions.Compiled);
,static readonly Regex reLabel2 = new Regex(",\\s*", RegexOptions.Compiled);
,static readonly Regex reContent = new Regex("┃([^┃]*)┃", RegexOptions.Compiled);
,static readonly Regex reDelims = new Regex("\\r?\\n", RegexOptions.Compiled);
,public static string[] Format(string input)
,{
,,List<string> B = new List<string>();
,,List<string> C = new List<string>();
,,List<string> CLocal = new List<string>();
,,List<string> keysInContent = new List<string>();//#keys in content
,,Dictionary<string, int> dicLabelCtr = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
,,OrderedDictionary dicLabelLocal4B = new OrderedDictionary();
,,//# label
,,MatchEvaluator sbLabel = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelCtr.ContainsKey(k))
,,,{
,,,,int ctr = ++dicLabelCtr[k];
,,,,return string.Format("┃Y_{0}{1}┃{2}{0}{1}[]", k, ctr, m.Groups[2].Value);
,,,}
,,,else
,,,{
,,,,dicLabelCtr[k] = 1;
,,,,return string.Format("┃Y_{0}┃{1}{0}[]", k, m.Groups[2].Value);
,,,}
,,};
,,//# content
,,MatchEvaluator sbContent = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelLocal4B.Contains(k))
,,,{
,,,,keysInContent.Add(k);
,,,,if (dicLabelCtr.ContainsKey(k))
,,,,{
,,,,,return string.Format("┃Y_{0}{1}┃", k, dicLabelCtr[k] + 1);
,,,,}
,,,,else
,,,,{
,,,,,return string.Format("┃Y_{0}┃", k);
,,,,}
,,,}
,,,else
,,,{
,,,,return string.Format("┃N_{0}┃", k);
,,,}
,,};
,,int stack = 0;
,,foreach (var line in reDelims.Split(input))
,,{
,,,if (stack == 0)
,,,{
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,stack = 1;
,,,,,//# procossing prev section
,,,,,for (int i = 0; i < CLocal.Count; i++)
,,,,,{
,,,,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,,,,}
,,,,,foreach (var key in dicLabelLocal4B.Keys)
,,,,,{
,,,,,,if (!keysInContent.Contains((string)key))
,,,,,,{
,,,,,,,C.Add((string)dicLabelLocal4B[key]);
,,,,,,}
,,,,,,else
,,,,,,{
,,,,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,,,,}
,,,,,}
,,,,,C.AddRange(CLocal);
,,,,,CLocal.Clear();
,,,,,keysInContent.Clear();
,,,,,dicLabelLocal4B.Clear();
,,,,,//# next section
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,,else
,,,{
,,,,//#stack=1
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,stack = 0;
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,}
,,//# procossing prev section
,,for (int i = 0; i < CLocal.Count; i++)
,,{
,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,}
,,foreach (var key in dicLabelLocal4B.Keys)
,,{
,,,if (!keysInContent.Contains((string)key))
,,,{
,,,,C.Add((string)dicLabelLocal4B[key]);
,,,}
,,,else
,,,{
,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,}
,,}
,,C.AddRange(CLocal);
,,CLocal.Clear();
,,keysInContent.Clear();
,,dicLabelLocal4B.Clear();
,,//return result
,,return new string[] { string.Join(Environment.NewLine, B.ToArray()), string.Join(Environment.NewLine, C.ToArray()) };
,}
}

2. Or create C# object and call its functions. Example: in CsScript.Call help click link "create object".

Macro Macro3242
Code:
Copy      Help
_s=
;┋Country, United States, United Kingdom, France
;My name is ┃Name┃, My country is ┃Country┃, My age is ┃Age┃, thank you!

CsScript x
x.AddCode("")
IDispatch o=x.CreateObject("Formater")
str B C
BSTR bB bC
o.Format(_s &bB &bC)
B=bB
C=bC
out "B:"
out B
out "C:"
out C

#ret
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class Formater
{
,static readonly Regex reLabel = new Regex("┋([^,]*),(\\s*)", RegexOptions.Compiled);
,static readonly Regex reLabel2 = new Regex(",\\s*", RegexOptions.Compiled);
,static readonly Regex reContent = new Regex("┃([^┃]*)┃", RegexOptions.Compiled);
,static readonly Regex reDelims = new Regex("\\r?\\n", RegexOptions.Compiled);
,public void Format(string input, ref string rB, ref string rC)
,{
,,List<string> B = new List<string>();
,,List<string> C = new List<string>();
,,List<string> CLocal = new List<string>();
,,List<string> keysInContent = new List<string>();//#keys in content
,,Dictionary<string, int> dicLabelCtr = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
,,OrderedDictionary dicLabelLocal4B = new OrderedDictionary();
,,//# label
,,MatchEvaluator sbLabel = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelCtr.ContainsKey(k))
,,,{
,,,,int ctr = ++dicLabelCtr[k];
,,,,return string.Format("┃Y_{0}{1}┃{2}{0}{1}[]", k, ctr, m.Groups[2].Value);
,,,}
,,,else
,,,{
,,,,dicLabelCtr[k] = 1;
,,,,return string.Format("┃Y_{0}┃{1}{0}[]", k, m.Groups[2].Value);
,,,}
,,};
,,//# content
,,MatchEvaluator sbContent = delegate (Match m)
,,{
,,,string k = m.Groups[1].Value;
,,,if (dicLabelLocal4B.Contains(k))
,,,{
,,,,keysInContent.Add(k);
,,,,if (dicLabelCtr.ContainsKey(k))
,,,,{
,,,,,return string.Format("┃Y_{0}{1}┃", k, dicLabelCtr[k] + 1);
,,,,}
,,,,else
,,,,{
,,,,,return string.Format("┃Y_{0}┃", k);
,,,,}
,,,}
,,,else
,,,{
,,,,return string.Format("┃N_{0}┃", k);
,,,}
,,};
,,int stack = 0;
,,foreach (var line in reDelims.Split(input))
,,{
,,,if (stack == 0)
,,,{
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,stack = 1;
,,,,,//# procossing prev section
,,,,,for (int i = 0; i < CLocal.Count; i++)
,,,,,{
,,,,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,,,,}
,,,,,foreach (var key in dicLabelLocal4B.Keys)
,,,,,{
,,,,,,if (!keysInContent.Contains((string)key))
,,,,,,{
,,,,,,,C.Add((string)dicLabelLocal4B[key]);
,,,,,,}
,,,,,,else
,,,,,,{
,,,,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,,,,}
,,,,,}
,,,,,C.AddRange(CLocal);
,,,,,CLocal.Clear();
,,,,,keysInContent.Clear();
,,,,,dicLabelLocal4B.Clear();
,,,,,//# next section
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,,else
,,,{
,,,,//#stack=1
,,,,if (line.StartsWith("┋"))
,,,,{
,,,,,Match m = reLabel.Match(line);
,,,,,if (m.Success)
,,,,,{
,,,,,,dicLabelLocal4B[m.Groups[1].Value] = line;
,,,,,}
,,,,,else
,,,,,{
,,,,,,dicLabelLocal4B[line] = line;
,,,,,}
,,,,}
,,,,else
,,,,{
,,,,,stack = 0;
,,,,,CLocal.Add(line);
,,,,}
,,,}
,,}
,,//# procossing prev section
,,for (int i = 0; i < CLocal.Count; i++)
,,{
,,,CLocal[i] = reContent.Replace(CLocal[i], sbContent);
,,}
,,foreach (var key in dicLabelLocal4B.Keys)
,,{
,,,if (!keysInContent.Contains((string)key))
,,,{
,,,,C.Add((string)dicLabelLocal4B[key]);
,,,}
,,,else
,,,{
,,,,B.Add(reLabel2.Replace(reLabel.Replace((string)dicLabelLocal4B[key], sbLabel), "[]"));
,,,}
,,}
,,C.AddRange(CLocal);
,,CLocal.Clear();
,,keysInContent.Clear();
,,dicLabelLocal4B.Clear();
,,//return result
,,rB=string.Join(Environment.NewLine, B.ToArray());
,,rC=string.Join(Environment.NewLine, C.ToArray());
,}
}
#4
@Gintaras
I learned a lot, thank you so much
#5
every day, The first time use C# code, it's a bit slow, and then it's pretty quick
Where is the first compiled file?  How to use it directly
#6
To compile C# code QM2 executes csc.exe. Can't make faster.
#7
Thanks for the reminder

I have a habit of using my computer to clean up temporary files every time I shut it down
I found the dll file in the temporary folder
"C:\Users\Administrator\AppData\Local\Temp\QM\CsScript\94A66782A85188ED.dll"

Can I put this dll file directly into the QM folder and reference it? How to reference it in QM2
I think this will probably keep the startup speed consistent
#8
No.


Forum Jump:


Users browsing this thread: 1 Guest(s)