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
sample.MyPoint.X = 10;
sample.MyPoint.Y = 20;
sample.MyString = "Hello, World";
sample.MyIntSequence.AddRange(new int[] { 1, 2, 4, 5 });
sample.MyPointSequence.Add(new Point(X: 10, Y: 20));
for (int i = 0; i < sample.MyPointArray.Length; i++)
{
sample.MyPointArray[i].X = i;
sample.MyPointArray[i].Y = i;
}
var sampleCopy = new MyType(sample);
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
@ 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;
string readableSample = sample.ToString();
Console.WriteLine(readableSample);
sample,
Console.WriteLine(jsonSample);
string xmlSample = MyTypeSupport.Instance.ToString(
sample,
new PrintFormatProperty
{
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;
ISerializer<MyType> serializer = MyTypeSupport.Instance.CreateSerializer();
byte[] sampleBuffer = serializer.Serialize(sample);
MyType deserializedSample = serializer.Deserialize(sampleBuffer);
Debug.Assert(deserializedSample.Equals(sample));
Example: Get the dynamic type definition of an IDL type
DynamicType myType = MyTypeSupport.Instance.DynamicType;
Console.WriteLine(myType.Name);
StructType myStruct = (StructType) myType;
StructMember member = myStruct.GetMember("MyKey");
Debug.Assert(member.IsKey);
DynamicData dynamicSample = new DynamicData(myType);
dynamicSample.SetValue("MyKey", 10);
dynamicSample.SetValue("MyPoint.X", 10);
TypeKind
The different kinds of DynamicType.
Definition: TypeKind.cs:22