C
C#10mo ago
DaVinki

❔ ✅ Display all properties of different types without an attribute in a WPF list?

I have a Vehicle object that looks like this:
public class Vehicle
{
[DoNotLogInCsv]
private ILogger? Logger { get; }
[DoNotLogInCsv]
public VehicleProfile Profile { get; }
public DescribedValue<float> Rpm { get; private set; }
public DescribedValue<int> IntakeManifoldAbsolutePressure { get; private set; }
public DescribedValue<float> EngineLoad { get; private set; }
public DescribedValue<int> FuelPressure { get; private set; }
public DescribedValue<float> ThrottlePosition { get; private set; }
public DescribedValue<float> TimingAdvance { get; private set; }
public DescribedValue<float> IntakeAirTemperature { get; private set; }
public DescribedValue<float> ShortTermFuelTrimBank1 { get; private set; }
public DescribedValue<float> LongTermFuelTrimBank1 { get; private set; }
public DescribedValue<int> VehicleSpeed { get; private set; }
public DescribedValue<float> MafAirFlowRate { get; private set; }
public DescribedValue<int> RuntimeSinceEngineStart { get; private set; }
public DescribedValue<float> CommandedAirFuelEquivalencyRatio { get; private set; }
public DescribedValue<int> EngineOilTemperature { get; private set; }
public DescribedValue<float> FuelInjectionTiming { get; private set; }
}
public class Vehicle
{
[DoNotLogInCsv]
private ILogger? Logger { get; }
[DoNotLogInCsv]
public VehicleProfile Profile { get; }
public DescribedValue<float> Rpm { get; private set; }
public DescribedValue<int> IntakeManifoldAbsolutePressure { get; private set; }
public DescribedValue<float> EngineLoad { get; private set; }
public DescribedValue<int> FuelPressure { get; private set; }
public DescribedValue<float> ThrottlePosition { get; private set; }
public DescribedValue<float> TimingAdvance { get; private set; }
public DescribedValue<float> IntakeAirTemperature { get; private set; }
public DescribedValue<float> ShortTermFuelTrimBank1 { get; private set; }
public DescribedValue<float> LongTermFuelTrimBank1 { get; private set; }
public DescribedValue<int> VehicleSpeed { get; private set; }
public DescribedValue<float> MafAirFlowRate { get; private set; }
public DescribedValue<int> RuntimeSinceEngineStart { get; private set; }
public DescribedValue<float> CommandedAirFuelEquivalencyRatio { get; private set; }
public DescribedValue<int> EngineOilTemperature { get; private set; }
public DescribedValue<float> FuelInjectionTiming { get; private set; }
}
A described value is just a string Description and T Value pair object I want to get all of these properties without the CSV logging attribute to be shown in a WPF list. Any ideas on how I can do this? Couldn't come up with anything after a few hours and can't find much online.
3 Replies
Denis
Denis10mo ago
Have you tried reflection 🪞?
DaVinki
DaVinki10mo ago
I did end up using reflection as you suggested with a nongeneric base type first that ended up looking like this:
public record DescribedValue<T> : DescribedValue
where T : new()
{
public T Value { get; set; }
public override object? ObjectValue => Value;

public DescribedValue(string description, T value) : base(description)
{
Value = value;
}

public DescribedValue(string description) : base(description)
{
Value = typeof(T).IsValueType ? default! : new T();
}

public DescribedValue() : this("No description")
{
}

public override string ToString()
{
return $"{Description}: {Value}";
}
}

public abstract record DescribedValue(string Description)
{
public string Description { get; set; } = Description;

public abstract object? ObjectValue { get; }
}
public record DescribedValue<T> : DescribedValue
where T : new()
{
public T Value { get; set; }
public override object? ObjectValue => Value;

public DescribedValue(string description, T value) : base(description)
{
Value = value;
}

public DescribedValue(string description) : base(description)
{
Value = typeof(T).IsValueType ? default! : new T();
}

public DescribedValue() : this("No description")
{
}

public override string ToString()
{
return $"{Description}: {Value}";
}
}

public abstract record DescribedValue(string Description)
{
public string Description { get; set; } = Description;

public abstract object? ObjectValue { get; }
}
The boxing kind of sucks considering this is going to be hot but oh well, not really an issue compared to SerialPort IO speeds which are the main bottleneck. This is where the reflection was:
private List<DescribedValue>? _allObd2Properties;
public IReadOnlyList<DescribedValue> AllObd2Properties
{
get
{

_allObd2Properties ??= typeof(Vehicle)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof(DoNotLogInCsvAttribute)))
.Select(p => p.GetValue(this))
.Cast<DescribedValue>()
.ToList();

return _allObd2Properties;
}
}
private List<DescribedValue>? _allObd2Properties;
public IReadOnlyList<DescribedValue> AllObd2Properties
{
get
{

_allObd2Properties ??= typeof(Vehicle)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof(DoNotLogInCsvAttribute)))
.Select(p => p.GetValue(this))
.Cast<DescribedValue>()
.ToList();

return _allObd2Properties;
}
}
Thank you for the help
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Need help urgently with production app not behaving as expectedthis issue is with a WPF app: `App.ShutdownMode` is equal to `ShutdownMode.OnLastWindowClose`. When❔ New to C#, trying to create a gui that lets me start/stop my discord bot on a remote linux machineI am new to c#, but trying to learn. I have a simple gui with a start and stop button at the moment.❔ I can't fill my inputs when I click the "update" buttonIn order not to write it again, I am giving the stack overflow link. I will be grateful if you could❔ I don't know where to put handle error in my projectI use DDD in my project and this project makes CRUD. In infrastructure, there are a logic to call a ❔ Using XML to modify Nuget nuspec filesI am trying to take data from my old nuget packages nuspec file then I want to get the data from ea✅ Can't have interface with abstract static method as parameter in blazor componentI have interface and class like this: ```cs interface IFoo { static abstract IFoo Create(); void❔ Authentication on a ASP.NET application hosted on IISHi, I'm very new to deployment on IIS, and I'm very sure that it's just some configuration that isn'❔ newbie here. how to make combobox display values from 2d array 2nd column, and do nothing if null?this is visual studio windows form app combobox so i have a combobox called category. if i click the❔ Process Start with unicode charactersHello, I'm having issues with the following code to launch process when they are on directories wit❔ Uno using c# consolebasically as a school project we were asked to make a uno game code with certain rules, Me and my te