RTI Connext C# API  6.1.2

How to use C# classes generated from IDL by rtiddsgen.

How to use C# classes generated from IDL by rtiddsgen.

These examples use the topic-type Example.MyType.

The documentation of Example.MyType explains how rtiddsgen maps IDL types to C#. The mapping is based on the OMG IDL4-CSHARP specification.

To see how to publish and subscribe to a topic with an IDL type, see Publication examples and Subscription examples.

Sections:

Creating and populating a data sample

Example: Populate a data sample

// Create a sample with default values. It does a deep initialization
// of all its members.
var sample = new Example.MyType();
// Modify the sample properties:
sample.MyKey = 5;
sample.MyPoint.X = 10;
sample.MyPoint.Y = 20;
sample.MyString = "Hello, World";
// Add elements to a sequence:
sample.MyIntSequence.AddRange(new int[] { 1, 2, 4, 5 });
sample.MyPointSequence.Add(new Point(X: 10, Y: 20));
// Edit the elements of an array. Arrays have a fixed number of elements
for (int i = 0; i < sample.MyPointArray.Length; i++)
{
sample.MyPointArray[i].X = i;
sample.MyPointArray[i].Y = i;
}
// Create a deep copy:
var sampleCopy = new MyType(sample);
// ToString(), Equals and GetHashCode are overridden:
Debug.Assert(sampleCopy.Equals(sample));
int hashCode = sample.GetHashCode();
Console.WriteLine($"sample: {sample} - hashCode: {hashCode}");
Example C# class generated from the IDL struct MyType
Definition: MyType.cs:135
int MyKey
Gets or sets the value for the field MyKey
Definition: MyType.cs:216
Definition: MyType.cs:17
@ Debug
The message contains debug information that might be relevant to your application.

Advanced TypeSupport operations

Each IDL-based type has an associated TypeSupport class that provides utilities such as string formatting and serialization methods.

Example: Convert a data sample to JSON or XML

var sample = new MyType();
sample.MyPoint.X = 10;
// ...
// MyType.ToString() returns a readable string using default format settings
string readableSample = sample.ToString();
Console.WriteLine(readableSample);
// For additional options, use the MyTypeSupport, which is generated with
// rtiddsgen.
string jsonSample = Example.MyTypeSupport.Instance.ToString(
sample,
new PrintFormatProperty { Kind = PrintFormatKind.Json });
Console.WriteLine(jsonSample);
string xmlSample = MyTypeSupport.Instance.ToString(
sample,
new PrintFormatProperty
{
Kind = PrintFormatKind.Xml,
PrettyPrint = false
});
Console.WriteLine(xmlSample);
Provides utilities for MyType (this class is a singleton).
Definition: MyTypePlugin.cs:213
static MyTypeSupport Instance
The singleton instance
Definition: MyTypePlugin.cs:223
string ToString(T sample)
Converts a data sample into a readable string.
Definition: TypeSupport.cs:68
PrintFormatKind
Formats available when converting data samples to string representations.
Definition: PrintFormatProperty.cs:103

Example: Serialize a data sample into a byte buffer

var sample = new MyType();
sample.MyPoint.X = 10;
// ...
// Create a serializer.
//
// Warning: a serializer is not thread-safe. To use a serializer in
// multiple thread, either protect its operations or create a different
// serializer per thread.
ISerializer<MyType> serializer = MyTypeSupport.Instance.CreateSerializer();
// Serialize the data sample into a byte buffer
byte[] sampleBuffer = serializer.Serialize(sample);
// Create a new MyType object from the byte buffer
MyType deserializedSample = serializer.Deserialize(sampleBuffer);
Debug.Assert(deserializedSample.Equals(sample));

Example: Get the dynamic type definition of an IDL type

// Each type generated from IDL has a DynamicType definition available:
DynamicType myType = MyTypeSupport.Instance.DynamicType;
Console.WriteLine(myType.Name);
// We know it's a struct type:
Debug.Assert(myType.Kind == TypeKind.Structure);
StructType myStruct = (StructType) myType;
// We can inspect the type:
StructMember member = myStruct.GetMember("MyKey");
Debug.Assert(member.IsKey); // MyKey has the @key annotation in IDL
// We can use it to create a DynamicData sample:
DynamicData dynamicSample = new DynamicData(myType);
dynamicSample.SetValue("MyKey", 10);
dynamicSample.SetValue("MyPoint.X", 10);
TypeKind
The different kinds of DynamicType.
Definition: TypeKind.cs:22