Can I pass a PHP associative array to a ComVisible .NET function? - c#

When I pass a PHP indexed array:
$myVar = new COM('MyClass');
$myVar->DubbleArray([1, 2]);
...it comes through to C# or F# as an System.Object[], and I can deal with it accordingly, e.g.:
[<ComVisible(true)>]
type LettersClass() =
member this.DubbleArray(array: obj[]) =
// do stuff with the values in the array
But when I pass an associative array:
$myVar->DubbleArray(array('first'=>'1st','second'=>'2nd'));
...it comes through as DBNull.
Is there any way to pass an associative array to a .NET function via COM?

Using a Dictionary in .NET allows you to deal with named keys. You could always encode the data in JSON format then decode in .NET as well... :)
(Note: this answer is based off of not seeing any .net in your question)

I suppose it really falls on how marshalling of an associative array is handled on the PHP side. The actual answer very well might be "not at all".
Unless you can figure that out, I would suggest a workaround - my best bet would be to convert the associative array to a multidimensional array storing keys and values in separate columns, and passing that through as an obj[,].

Related

Deserialize anonymous jagged JSON array c#

I have an API endpoint which is receiving a data string which looks like the following:
"[[0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0],[[],[],[],[[17.637329000012972,17.637329000012972]],[],[],[],[],[]]]"
I would now like to convert this json object into an array which I can query, but I'm not sure where to start.
I have tried converting to a jagged array using
var result = JsonConvert.DeserializeObject<SomeType[][]>(json); where SomeType has been int, string, double etc etc
The problem seems to lie with each array being anonymous so it's proving difficult to model. To further complicate things some of the arrays found in the base array are also jagged arrays. As it happens I'm only really interested in the first array so if anyone can offer an easy way of extracting this I would be very grateful. Thanks in advance

Reading binary file from delphi

I want to transition some old files first to human readable type, so in Delphi code is following:
OpenFileWriteRA(MyF, dd+'invoice.mfs', SizeOf(TFSerialDocEx)) then
and then calling
ReadFile(MyF, vSS1, SizeOf(TFSerialDocEx), nr1, nil);
So i am looking for a way to conver this files using with small programm i want to make it with C#, as i am more fammiliar with C# than with Delphi. .MFS file is written in binary, so what would i need to convert this to text/string, i tryed with simple binary convert but it was not ok, as it seems SizeOf Object at paramters is big thing here or?
There broadly speaking are three approaches that I would consider:
1. Transform data with Delphi code
Since you already have Delphi code to read the data, and structures defined, it will be simplest and quickest to transform the data with Delphi code. Simply read it using your existing code and then output in human readable form. For instance using the built in JSON libraries.
2. Define an equivalent formatted C# structure and blit the binary data onto that structure
Define a formatted structure in C# that has identical binary layout to the structure put to disk. This will use LayoutKind.Sequential and perhaps specify Pack = 1 if the Delphi structure is packed. You may need to use the MarshalAs attribute on some members to achieve binary equivalence. Then read the structure from disk into a byte array. Pin this array, and use Marshal.PtrToStructure on the pinned object address to deserialize. Now you have the data, you can write it how you please.
An example can be found here: Proper struct layout from delphi packed record
3. Read the structure field by field with a binary reader
Rather than declaring a binary compatible structure you can use a BinaryReader to read from a stream one field at a time. Method calls like Read, ReadInt32, ReadDouble, etc. let you work your way through the record. Remember that the fields will have been written in the order in which the Delphi record was declared. If the original record is aligned rather than packed you will need to step over any padding. Again, once you have the data available to your C# code you can write it as you please.

C# using a variable as a pointer to a location in 2D array

First of all I’m NOT a seasoned programmer and I’m learning C# on the fly for a project at work.
Without going into the overall details of the project, it basically is an analytic calculator for generating thermal vs time data for a semiconductor junction. The “user” inputs will be loaded from text file that contain names of a parameters along with values. Example “Rth_1 ,0.023”
Since I don’t have control of the actual placement of each parameter in the text file it is necessary to sort as I load all data into a 2D array as it is read from the text file and then keep track of where each parameter is in the 2D array.
So what I’m wanting to do is use a variable or pointer to certain location.
Something like “Param1 = [12],[2]”
Is this possible in C#?
Technically, you can use several approaches for having "single" pointer to the cell in 2D array. The most straightforward is to use Point class. Well, Point is a position in a discrete space after all.
Another approach is to use bit shifting if you know that your array will not be more than 2^32 or 2^16. Then just use i<<16+j to store the pointer.
Or you can use KeyValuePair<int, int>. Or Tuple<int, int>.

Fastest way to serialize C# object array into string

I am looking for the fastest way to serialize and deserialize a C# array of objects into a string...
Why a string and not a byte array? Well, I am working with a networking system (The Unity3d networking system to be specific) and they have placed a rather annoying restriction which does not allow the sending of byte arrays or custom types, two things I need (hard to explain my situation).
The simplest solution I have come up with for this is to serialize my custom types into a string, and then transmit that string as opposed to directly sending the object array.
So, that is the question! What is the fastest way to serialize an object array into a string? I would preferably like to avoid using voodoo characters (invisible/special characters), as I am not sure if Unity3d will cull them, but base64 encoding doesn't take full advantage of the allowed character spectrum. I am also worried about the efficiency of using base 64.
Obviously, since this is networking related, having the serialized data be as small as possible is a plus.
EDIT:
One possible way to do this would be to serialize to a byte array, and then pretend that that byte array is a string. The problem is, I am afraid that .Net 2.0, or Unity's networking system will end up culling some of the special or invisible characters created using this method... Something which very much needs to be avoided. I am hoping for a solution that has near or equal speed to this, but does not use any of the characters that are likely to be culled. (I have no idea what characters these are, but I have had bad experiences with Unity when it came to direct conversions to strings from byte arrays)
Json.Net is what I always use its simple and gets the job done in a human readable way. Json is about as lightweight as it gets and is widely used for sending data over the wire.
I'll give you this answer as accepted, but I suggest adding base64 encoding to your answer!
–Georges Oates Larsen
Thank you, and yes that is also a great option if readability is not an issue.
We use SoapFormatter so that the object be embedded in Javascript variables and otherwise be "safe" to pass around:
using (MemoryStream oStream = new MemoryStream())
{
(new SoapFormatter()).Serialize(oStream, theObject);
return Encoding.Default.GetString(oStream.ToArray());
}
using(MemoryStream s = new MemoryStream()) {
new BinaryFormatter().Serialize(s, obj);
return Convert.ToBase64String(s.ToArray());
}
and
using(MemoryStream s = new MemoryStream(Convert.FromBase64String(str))) {
return new BinaryFormatter().Deserialize(s);
}

What does this mean exactly? $_SESSION['sessionName '][$item['rowName ']]

And how does it translate to in C#?
$item holds the results from mysql_fetch_array()
I'm not really familiar with PHP so this is all new to me. Thanks.
$_SESSION is a superglobal that stores session data in an associative array, see http://php.net/session.
In your example, the value at $_SESSION['sessionName'] is apparently an array itself, which is indexed into with the value of $item['rowName'] which smells like a string.
To simplify the expression, you could define these variables:
$sessionName = $_SESSION['sessionName'];
$rowName = $item['rowName'];
And then we could say that your example code is equivalent to
$sessionName[$rowName]

Categories

Resources