Let's say I have the following interface:
public interface IStateInformation
{
public int TaskId { get; set; }
public string Name { get; set; }
}
and the following class implementing this interface:
public class ExternalStateInformation : IStateInformation
{
public int TaskId { get; set; }
public string Name { get; set; }
public string External { get; set; }
}
Now I want to write the following class to a CSV:
public class SwitchingData
{
public string DateTime { get; set; }
public int EpisodeId { get; set; }
public IStateInformation SourceState { get; set; }
public IStateInformation TargetState { get; set; }
}
As you can see, this class includes IStateInformation objects. When I try to write a record to a CSV file, only the properties of IStateInformation are considered (TaskId and Name) even when e.g. SourceState is of type ExternalStateInformation (therefore External is missing). My question is, how can I write the properties of a dynamic type instead of the static one?
I tried to use ClassMap but without success.
Details
The writing script looks like this:
...
using (var stream = File.Open(path, FileMode.Append))
using (var writer = new StreamWriter(stream))
using (var csv = new CsvWriter(writer, config))
{
if(type != null)
{
csv.Context.RegisterClassMap(type);
}
csv.Context.TypeConverterOptionsCache.AddOptions<DateTime>(options);
csv.Context.TypeConverterOptionsCache.AddOptions<DateTime?>(options);
csv.WriteRecords(data);
}
...
where type was the ClassMap I played with.
In the end I have written my own functions writing the header and the records. My given example would lead to the following header:
DateTime,EpisodeId,SourceState_TaskId,SourceState_Name,SourceState_External,TargetState_TaskId,TargetState_Name,TargetState_ExternalIf
TargetStatewould implement theIStateInformationinterface with another additional property, let's sayInternal, the header would look like:DateTime,EpisodeId,SourceState_TaskId,SourceState_Name,SourceState_External,TargetState_TaskId,TargetState_Name,TargetState_External,TargetState_InternalGeneral Writing Function
Function to get the Structure of the Object
This is needed to write default values instead of null values which would lead to too less CSV fields.
Check for ReferenceConverter
Header
Records