Click here to Skip to main content
15,889,767 members
Home / Discussions / C#
   

C#

 
QuestionIs there a JSON Library that works like the System.Xml.Linq classes? Pin
MAIsw30-May-18 21:02
MAIsw30-May-18 21:02 
AnswerRe: Is there a JSON Library that works like the System.Xml.Linq classes? Pin
Pete O'Hanlon30-May-18 22:31
mvePete O'Hanlon30-May-18 22:31 
QuestionCreate JSON object from string Pin
WillyBilly9030-May-18 10:27
WillyBilly9030-May-18 10:27 
AnswerRe: Create JSON object from string Pin
Mycroft Holmes30-May-18 14:17
professionalMycroft Holmes30-May-18 14:17 
GeneralRe: Create JSON object from string Pin
WillyBilly9031-May-18 9:57
WillyBilly9031-May-18 9:57 
AnswerRe: Create JSON object from string Pin
Richard Deeming31-May-18 10:08
mveRichard Deeming31-May-18 10:08 
GeneralRe: Create JSON object from string Pin
WillyBilly901-Jun-18 8:13
WillyBilly901-Jun-18 8:13 
QuestionReflect Generic Property Info Pin
Kevin Marois30-May-18 7:19
professionalKevin Marois30-May-18 7:19 
This is a bit long but please bear with me as I'm stuck. This is a C#/WPF app. The problem is C# so I posted this here.

I am creating a Property Sheet user control. Think of the Visual Studio property sheet. The first pass will simply be a two-column grid with a Caption in the left column and a UI element in the right columm (TextBox, CheckBox, ComboBox).

This control will embedded into a larger UI that allows the user to change the settings used int a Testing Application, without the need to recompile the app.

Data
The settings will be serialized/deserialized in an XML file:
<_TestSettingsBase xsi:type="BeadBeatingMotorSettings">
<TestName>Bead Beating</TestName>
<TestType>BeadBeating</TestType>
<BeadBeaterMax>
  <Caption>Bead Beater Max</Caption>
  <Description>Bead Beater maximum</Description>
  <CurrentValue>
    <Value>1</Value>
    <Value1>1</Value1>
  </CurrentValue>
  <DefaultValue>
    <Value>1</Value>
    <Value1>1</Value1>
  </DefaultValue>
  <ValueMax>
    <Value>0</Value>
    <Value1>0</Value1>
  </ValueMax>
  <ValueMin>
    <Value>100</Value>
    <Value1>100</Value1>
  </ValueMin>
</BeadBeaterMax>
<BeadBeaterMin>
  <Caption>Bead Beater Min</Caption>
  <Description>Bead Beater minimum</Description>
  <CurrentValue>
    <Value>0.075</Value>
    <Value1>0.075</Value1>
  </CurrentValue>
  <DefaultValue>
    <Value>0.075</Value>
    <Value1>0.075</Value1>
  </DefaultValue>
  <ValueMax>
    <Value>101</Value>
    <Value1>101</Value1>
  </ValueMax>
  <ValueMin>
    <Value>999</Value>
    <Value1>999</Value1>
  </ValueMin>
</BeadBeaterMin>
</_TestSettingsBase>
In the XML above the base setting class has TestName and TestType properties
[Serializable]
public class _TestSettingsBase
{
    public string TestName { get; set; }

    public TestType TestType { get; set; }
}
Then, each individual Test class will inherit from that
[Serializable]
public class BeadBeatingMotorSettings : _TestSettingsBase
{
    [ShowInPropertySheet()]
    public Setting BeadBeaterMax { get; set; }

    [ShowInPropertySheet()]
    public Setting BeadBeaterMin { get; set; }

    // CTOR
    public BeadBeatingMotorSettings()
    {
        TestType = TestType.BeadBeating;

        BeadBeaterMax = new Setting
        {
            Caption = "Bead Beater Max",
            CurrentValue = 1,
            DefaultValue = 1,
            Description = "Bead Beater maximum",
            ValueMax = 0,
            ValueMin = 100
        };

        BeadBeaterMin = new Setting
        {
            Caption = "Bead Beater Min",
            CurrentValue = 0.075,
            DefaultValue = 0.075,
            Description = "Bead Beater minimum",
            ValueMax = 101,
            ValueMin = 999
        };
    }
}
The test's properties are defined as a generic type property I called Setting<t>
[Serializable]
public class Setting : _EntityBase
{
    public string Caption { get; set; }

    public string Description { get; set; }

    public MyProp CurrentValue { get; set; }

    public MyProp DefaultValue { get; set; }

    public MyProp ValueMax { get; set; }

    public MyProp ValueMin { get; set; }
}

public class MyProp
{
    private T _value;
    public T Value
    {
        get { return Value1; }
        set { Value1 = value; }
    }

    public T Value1 { get => _value; set => _value = value; }

    public static implicit operator T(MyProp value)
    {
        return value.Value;
    }

    public static implicit operator MyProp(T value)
    {
        return new MyProp { Value = value };
    }
}
So far everything works great. The data is serialized/deserialized perfectly.

Property Sheet
This is where I'm having trouble.

The PropertySheet is a DataGrid with two columns. The left column is the property's caption and the and right column has a UI element to display the value (TextBox, CheckBox, ComboBox). So far, I created a small User Control for each type of data I expect to deal with.

For example, for Double, I have a UC called DoublePropertyView. It's XAML is this
<UserControl x:Class="PropSheet.PropertyViews.DoublePropertyView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="100" 
             d:DesignWidth="100">

    <TextBox Text="{Binding SettingValue}"/>

</UserControl>
The code behind has a DependencyProperty called "SettingValue" that the TextBox is bound to. Nothing more than that.

I have one small UC for each type of data - double, int, float, bool, string, etc.

Then the DataGrid's right column uses a TemplateSelector to determine which UC to load into the cell.

The PropertySheet UC has a DP called Settings that takes a list of _TestSettingsBase.

Problem
When I set the list of properties on the DP I then need to reflect each of the properties on the Test class and load into the grid only those properties with the custom attribute ShowInPropertySheet(true).

In the code above you can see on the BeadBeating test class there are two properties, BeadBeaterMin and BeadBeaterMax.

I want these two to appear in the property sheet. Notice that they're marked with the ShowInPropertySheet attribute. When shown in the property sheet I want the Caption and CurrentValue to appear.

Here's my LoadProperties code with the problem. I loop through each Test class in the Settings DP and create a model for each. The list of models is bound to the grid
private void LoadProperties()
{
    Properties = new List<PropertyModel>();

    // Get a list of PropertyInfos for each property on the setting marked with ShowInPropertySheetAttribute(true)
    var propertyInfos = from pi in Setting.GetType().GetProperties()
                        let attrs = pi.GetCustomAttributes(typeof(ShowInPropertySheetAttribute), true)
                        where attrs.Length != 0 &&
                        (bool)Helpers.GetAttributeValue(Setting.GetType(), pi.Name, typeof(ShowInPropertySheetAttribute), "IsVisible")
                        select pi;

    foreach (PropertyInfo propertyInfo in propertyInfos)
    {
        // THE PROBLEM IS HERE. I WON"T KNOW WHAT THE TYPE THE SETTING IS AT 
        // RUNTIME. EVEN THOUGH I HAVE "DOUBLE" HARDCODED HERE, I NEED TO GET 
        // THE TYPE FROM THE <T>  SET ON EACH OF THE PROPERTIES
        // MARKED WITH ShowInPropertySheet<br />
        var settingProperty = (Setting<double>)propertyInfo.GetValue(Setting);

        var current = settingProperty.CurrentValue;

        var model = new PropertyModel(propertyInfo.Name, current.Value);

        Properties.Add(model);
    }
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 30-May-18 13:59pm.

AnswerRe: Reflect Generic Property Info Pin
Gerry Schmitz30-May-18 9:46
mveGerry Schmitz30-May-18 9:46 
AnswerRe: Reflect Generic Property Info Pin
Richard Deeming30-May-18 9:51
mveRichard Deeming30-May-18 9:51 
GeneralRe: Reflect Generic Property Info Pin
Eddy Vluggen30-May-18 10:23
professionalEddy Vluggen30-May-18 10:23 
GeneralRe: Reflect Generic Property Info Pin
Kevin Marois30-May-18 10:30
professionalKevin Marois30-May-18 10:30 
GeneralRe: Reflect Generic Property Info Pin
Kevin Marois31-May-18 11:20
professionalKevin Marois31-May-18 11:20 
GeneralRe: Reflect Generic Property Info Pin
Richard Deeming31-May-18 11:52
mveRichard Deeming31-May-18 11:52 
QuestionUnable to connect to remote server error in iis express Pin
galba201830-May-18 3:07
galba201830-May-18 3:07 
AnswerRe: Unable to connect to remote server error in iis express Pin
Pete O'Hanlon30-May-18 5:31
mvePete O'Hanlon30-May-18 5:31 
GeneralRe: Unable to connect to remote server error in iis express Pin
galba201830-May-18 20:10
galba201830-May-18 20:10 
QuestionGet Outlook email attachments Pin
Raghu Kiran29-May-18 16:23
Raghu Kiran29-May-18 16:23 
AnswerRe: Get Outlook email attachments Pin
Pete O'Hanlon29-May-18 22:36
mvePete O'Hanlon29-May-18 22:36 
AnswerRe: Get Outlook email attachments Pin
Richard MacCutchan29-May-18 22:36
mveRichard MacCutchan29-May-18 22:36 
Questionuser contrôle with entity frameworks Pin
Yassine Soutsan28-May-18 3:29
Yassine Soutsan28-May-18 3:29 
AnswerRe: user contrôle with entity frameworks Pin
Dave Kreskowiak28-May-18 6:09
mveDave Kreskowiak28-May-18 6:09 
AnswerRe: user contrôle with entity frameworks Pin
User 418025429-May-18 2:02
User 418025429-May-18 2:02 
GeneralRe: user contrôle with entity frameworks Pin
Gerry Schmitz29-May-18 7:26
mveGerry Schmitz29-May-18 7:26 
QuestionMD file Pin
Member 1384619127-May-18 5:30
Member 1384619127-May-18 5:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.