Click here to Skip to main content
15,919,613 members
Home / Discussions / C#
   

C#

 
AnswerRe: JSON problem with backslash through REST service Pin
Richard Deeming4-Jun-18 5:53
mveRichard Deeming4-Jun-18 5:53 
Questionsend sms from c# windows application Pin
Mohamed Fahad M2-Jun-18 21:53
Mohamed Fahad M2-Jun-18 21:53 
AnswerRe: send sms from c# windows application Pin
OriginalGriff2-Jun-18 22:14
mveOriginalGriff2-Jun-18 22:14 
QuestionC sharp code for Objective and subjective CBT Exam Pin
Member 1293991431-May-18 0:39
Member 1293991431-May-18 0:39 
AnswerRe: C sharp code for Objective and subjective CBT Exam Pin
Richard MacCutchan31-May-18 0:47
mveRichard MacCutchan31-May-18 0:47 
AnswerRe: C sharp code for Objective and subjective CBT Exam Pin
OriginalGriff31-May-18 2:24
mveOriginalGriff31-May-18 2:24 
AnswerRe: C sharp code for Objective and subjective CBT Exam Pin
Gerry Schmitz31-May-18 11:43
mveGerry Schmitz31-May-18 11:43 
QuestionSERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Member 1368881630-May-18 22:19
Member 1368881630-May-18 22:19 
QuestionRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Richard MacCutchan30-May-18 23:00
mveRichard MacCutchan30-May-18 23:00 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Member 1368881630-May-18 23:29
Member 1368881630-May-18 23:29 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Jochen Arndt30-May-18 23:39
professionalJochen Arndt30-May-18 23:39 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Gerry Schmitz31-May-18 11:50
mveGerry Schmitz31-May-18 11:50 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Member 1368881631-May-18 23:04
Member 1368881631-May-18 23:04 
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 

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.