PDFsharp - C# - KeyValuePair - PowerShell conversion - c#

Looking at the sample of C# code (Here). I'm trying to work out how to convert the required KeyPair syntax into PowerShell. I have an example of working PowerShell code, which will use the PDFsharp assembly to create a file, I'd like to modify that script to also modify a Custom Property of the PDF file.

You don't actually need to create the KeyValuePair object manually, the Info property on PdfDocument has an Add() method that takes a string as the key and a PdfItem object as the value:
$propertyKey = "/MyKey"
$propertyValue = "MyValue"
$document.Info.Add($propertyKey, [PdfSharp.Pdf.PdfString]::new($propertyValue))

Related

C# Get an array to convert to a PSObject

I'm trying to use the System.Management.Automation namespace to use the built-in gridviewer from PowerShell in c#, however I can't seem to be able to get it to display my list of items, it always just returns it as one line as if I was trying to get the info of the array instead of it's members.
SomeClass[] list; //let's say I have a simple array of one object type
PowerShell posh = PowerShell.Create();
posh.AddCommand("Out-Gridview");
posh.AddArgument(list); //this inputs the list as first argument for the command
posh.Invoke(); //this now just opens a gridview with information about the list itself (length, syncroot, ...)
So what I would like is a way to translate my already existing list into a powershell-compatible object inside my C# code, preferably using only the automation library and other default .NET classes.

multilingual codeeffects rule editor

How can I render the CodeEffects Rule Editor in a different language (Let's say Arabic).
I am using a custom class as the source object for the rule model and passing that in the viewbag to the view, there i am rendering the rule editor using that. I have not explictely called any source xml or help xml doc. the rule editor is picking the default english version.
#{
Html.CodeEffects().RuleEditor()
.Id("ruleEditor")
.SaveAction("SaveGroup", "Campaign")
.DeleteAction("DeleteGroup", "Campaign")
.LoadAction("LoadGroup", "Campaign")
.Mode(RuleType.Evaluation)
.ToolBarRules(ViewBag.ToolBarRules)
.Rule(ViewBag.Rule)
.Render();
}
So far on CodeEffects official documentation I have come accross this Help XML and Multilingual Support in Code Effects but I couldn't understand it properly
How can I load a custom help xml and source xml file from the cshtml page using razor syntax
Any help would be appreciated.
In general, you simply create your own version of the Help XML in your language and pass it to the editor on the server using the editor's HelpXmlFile property during the editor initialization.
You also need to have your own version of the Source XML with all display names translated in your language if you want your fields, in-rule methods and rule actions to be displayed in your language on the client. You can pass that source xml to the editor using the its SourceXmlFile property.
Call editor.GetHelpXml() method to get the default version of the Help XML to be translated; call editor.GetSourceXML() to get the Source XML of your source object to be translated (pass your source class to your editor first before calling that method, of course.)
EDIT:
As I said earlier, you need to use your own custom version of the Help XML which sets language-specific labels of all static UI elements such as buttons, Help String, etc. To do that, create a default instance of the RuleEditor class and call its GetHelpXml() method. That gives youa string of the default English xml document. Translate all its node values to your language, save it and pass that new xml to the editor using one of the overloads of its Help() extension method. Details can be found here.
I assume that you also need to have all properties, in-rule methods and rule actions displayed in your language. You have two options here:
If your project only uses one language, then simply use the FieldAttribute, MethodAttribute and/or ActionAttribute to set their DisplayName properties to values in your language. Details are here, here, and here.
If your project uses multiple languages then create an instance of the RuleEditor class, set its SourceType property to the type of your source class and call its GetSourceXml() method whoch returns a string of the XML doc that represents your source class. Create a copy of that doc for each of the language your project uses, translate all value of the "displayName" attributes in correspondent language, save those files and pass the desired one to the RuleModel.Create() method when you init the editor on the server. Details are here.

Calling into python code using Pythonnet from C# and custom C# classes

I have the majority of my code base in c# where data is entered into a datagrid in a GUI and then this is taken into a set of python3 functions to interrogate the data further.
From the datagrid information , user defined objects containing boolean, float and dictionary properties are populated which are then passed into python.
Currently the python code is using only python functionality and I think this may be the issue as the dict.keys() method in the python funciton is not recognising the C# dictionary that is passed in.
Is there an easy way to convvert the c# dictionary into a python dictionary so that the python code can be unchanged?
Thanks!
A quick and dirty work around would be to use NewtonSoft nugget to serialize your class contents (in this case, your dictionary) and convert it into a JSON.
using Newtonsoft.Json
var myDictionary = new Dictionary<string,string>();
myDictionary.Add("key1","value1");
myDictionary.Add("key2","value2");
//Now convert the dictionary into json.
var jsonToSend = JsonConvert.SerializeObject(myDictionary);
Now pass in that jsonToSend to the python code and at the python code, load the json into a dict using json module like below:
import json
passedin_json = 'the output of your method that receives the json'
loaded_json_data = json.loads(passedin_json)
And done, Now you've successfully transferred your C# dictionary to your python dict

Dealing with a JSON.Net JArray in Python?

I'm using IronPython as a scripting language in my C# application. One of the "features" that I've implemented is the ability for a script to persist values, which are then exposed to the next script being executed. This is achieved by passing the value(s) to be stored to a C# class, exposed to the script by the "host" application. The code to store a value looks something like this:
store.set("xyz", 123)
('store' is the variable through which the C# object is exposed).
Internally the C# class stores these name/value pairs in a Dictionary<string, object>. When the script finishes executing it serialises the dictionary using Json.Net (var json = JsonConvert.SerializeObject(dict)) and writes the resulting string to file.
When the next script is run, the "host" C# application reads and deserialises the file (JsonConvert.DeserializeObject<Dictionary<string, object>>(s)) and exposes the name/value pairs to the script via the same C# class, which the script can access like this:
my_var = store.get("xyz")
This feature has been working fine with simple types such as ints and floats, but one of our users now needs to persist a list of ints. It works to a fashion - the list gets persisted and exposed to the next Python script, but at this point it is now a JArray type (something to do with Json.Net it seems). This doesn't play nicely with the Python code (which is expecting a list of ints).
I guess the simplest fix is to convert this JArray to a Python int list. But how?
Alternatively it would be nice if the issue could be "fixed" in the C# class (casting?), to avoid users from having to do this conversion in their scripts. However I don't want to change the de/serialisation process to the extent that it no longer reads users' existing data files. Thoughts?
I've found a solution with only minimal impact on script authors. In my C# code, after deserialising the file, I go through the dictionary looking for items with a value of type JArray, and convert them to arrays:
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
var keys = dict.Keys.ToList();
foreach (var key in keys)
{
var jarray = dict[key] as JArray;
if (jarray != null)
{
dict[key] = jarray.ToObject<object[]>();
}
}
These arrays are then exposed to the Python script as before; as they are C# arrays the script authoer must convert to Python lists, which is trivial:
my_var = list(store.get("xyz"))

How to pass a .cs parameter to Visual Studio WebTest QueryStringParameter?

I have created a webtest in visual studio to perform a test on a Rest API.I have some code that generates a random serial number that i need to pass into my QueryString parameter is there anyway to do this? I noticed that i can bind xml, csv and a database to it for Dynamic values. Would i have to change my code to wright to a xml file or is it possible to perform a direct call to the method inside the .cs file? C# Langauge.
I want to do something like in the below method but i want the sessionToken.sessionToken() to be the method that was called from the .cs file.
QueryString Parameters
generatedToken=sessionToken.sessionToken()
Write your code within the PreRequest method of a Web Test Request plugin, or call your method from the plugin. One of the last statements of the plugin should write the generated value to a context parameter, with code something like:
e.WebTest.Context["YourContextParameter"] = TheGeneratedValue.ToString();
The value of the query string parameter can then be in one of these styles
{{YourContextParameter}}
or
Some text{{YourContextParameter}}more text
With a little more effort you can pass the context parameter name as a parameter of the plugin.
It can be useful to add diagnostics from the plugin into the log; to showit has been called and to record its output. This can be done with a statement of the form:
e.WebTest.AddCommentToResult("Plugin result is " + TheGeneratedValue.ToString());
Another approach is to convert the entire webtest to a coded test, by using one of the command icons just above the webtest. That produces C# that you can edit as much as you want. However, that conversion is a one-way process. After doing it you cannot use the webtest editor to further change the test. Hence I recommend the plugin route.

Categories

Resources