LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Create a .NET ReadOnlySpan

Solved!
Go to solution

Hi,

 

I'm using an .NET assembly and everything goes well except for one method which takes a ReadOnlySpan as a input :

 

Capture.PNG

ReadOnlySpan<T> Constructeur (System) | Microsoft Learn

 

After many atempts with my colleagues we are stuck at creating that.

 

Any help or valuable idea ?

 

Thx

 

Sébastien
CLA, CTA
0 Kudos
Message 1 of 15
(583 Views)
Solution
Accepted by topic author SebastienM

There's the same problem with List<T>.

 

Here's how to make a List of Boolean:

 

dotNet Create List.png

 

Changing that string to your "ReadOnlySpan" should do the trick.

 

Once the instance is created, you can cast it.

 

You don't have to get the name from the assembly, but my type (Boolean is just for this example) might be upgraded, and I don't the code to break when it is updated.

Message 2 of 15
(548 Views)

Hello to all,

I have a similar problem. I also need to fill a System.Collection.Generic.List'1 list. I wanted to test it as described above. Unfortunately, I don't know what reference goes to the Type and Activator method in the example. I hope I can create the red framed reference with it.

I would also appreciate any tips.

Thanks already to all 😉

Test_GenericList.jpg

0 Kudos
Message 3 of 15
(453 Views)

Connect a string constant "System.Collection.Generic.List'1" in the type name input

Sébastien
CLA, CTA
0 Kudos
Message 4 of 15
(435 Views)

Hello,

I have now found the missing elements, but unfortunately I still do not know what to do.
The method Session.Read expects a reference to a Generic ListElement
(System.Collections.Generic.IList`1[[Softing.Opc.Ua.Toolkit.ReadValueId,Softing.Opc.Ua.Toolkit, Version=1.49.0.7691, Culture=neutral, PublicKeyToken=19132d4dfa11b90e]], mscorlib)

I was hoping that I could create this element using the construct. Unfortunately, I am not getting anywhere here. From the method Activator always comes an error

System.MissingMethodException: Cannot create an instance of an interface.

Does anyone have an idea how to create an instance of the list?

Thanks...

 

T_0-1666009600685.png

 

 

0 Kudos
Message 5 of 15
(432 Views)

@T. wrote:

Hello to all,

I have a similar problem. I also need to fill a System.Collection.Generic.List'1 list. I wanted to test it as described above. Unfortunately, I don't know what reference goes to the Type and Activator method in the example. 


The type you want...

 

In the example I've posted, a Boolean.

 

In your code, the creation of the string seems OK

 

However, you removed the code that actually creates the object. The while loop prevents 'calculating' the string over and over again (it won't change). (You could go even further and cache the .NET type).

 

Provided ReadValueID is a System.Object, the attached VI should work. Simply wire the ReadValueID to it.

 

Note you need to close both ReadValueID (once it's in the list) AND the List<ReadValueID> (once it's passed to Read).

 

Also note you still need to add the ReadValueID to the list, the VI only creates the type.

 

EDIT: That crossed...

 

0 Kudos
Message 6 of 15
(427 Views)

@T. wrote:

Does anyone have an idea how to create an instance of the list?


You are actually creating the list.

 

Not sure what you expect if you don't use the result.

 

The output of the activator should go into the Add method (to the top left, instead of the constant).

 

However, the constant should be used to cast (to more specific) the output to that type.

wiebeCARYA_0-1666010967854.png

 


@T. wrote:

Hello,

I have now found the missing elements, but unfortunately I still do not know what to do.
The method Session.Read expects a reference to a Generic ListElement
(System.Collections.Generic.IList`1[[Softing.Opc.Ua.Toolkit.ReadValueId,Softing.Opc.Ua.Toolkit, Version=1.49.0.7691, Culture=neutral, PublicKeyToken=19132d4dfa11b90e]], mscorlib)

I was hoping that I could create this element using the construct. Unfortunately, I am not getting anywhere here. From the method Activator always comes an error

System.MissingMethodException: Cannot create an instance of an interface.


EDIT:

Sorry I didn't get that. If the Activator returns an error, the rest won't matter.

 

Not sure what's happening here, but there's little we can do about that.

 

Without being able to try things, you're basically on your own.

0 Kudos
Message 7 of 15
(424 Views)

 

Hello,

This is how I would have imagined it. Unfortunately, an error always comes out of the CreateInstance method.

 

 

T_0-1666011222050.png

Thanks...

 

0 Kudos
Message 8 of 15
(418 Views)

@T. wrote:

 

Hello,

This is how I would have imagined it. Unfortunately, an error always comes out of the CreateInstance method.


Could you try removing ", mscorlib" from the type string? 

0 Kudos
Message 9 of 15
(414 Views)

@T. wrote:

This is how I would have imagined it. Unfortunately, an error always comes out of the CreateInstance method.


You also need to close the list and type references, or you'll leak memory.

0 Kudos
Message 10 of 15
(403 Views)