C# 如何仅从类中获取真实属性

C# 如何仅从类中获取真实属性,c#,C#,我有一个具有6个属性的类: public class ControllerValuesArgs:EventArgs { // debouncer for button private static int btnCounter = 0; // flag to send buttons bool activeFlag = false; /// <summary> /// Defau

我有一个具有6个属性的类:

public class ControllerValuesArgs:EventArgs
    {
        // debouncer for button
        private static int btnCounter = 0;
        // flag to send buttons
        bool activeFlag = false;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public ControllerValuesArgs()
        {
            // Reset buttons to initial state
            ResetButtons();
        }

        /// <summary>
        /// Gets or sets state of button 1.
        /// </summary>
        public bool Button1Pressed
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets state of button 2.
        /// </summary>
        public bool Button2Pressed
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets state of button 3.
        /// </summary>
        public bool Button3Pressed
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets state of button 4.
        /// </summary>
        public bool Button4Pressed
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets state of button 5.
        /// </summary>
        public bool Button5Pressed
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets state of button 6.
        /// </summary>
        public bool Button6Pressed
        {
            get;
            set;
        }
公共类ControllerValuesArgs:EventArgs
{
//按钮去抖动器
专用静态int btnCounter=0;
//发送按钮的标志
bool-activeFlag=false;
/// 
///默认构造函数。
/// 
公共控制价值
{
//将按钮重置为初始状态
重置按钮();
}
/// 
///获取或设置按钮1的状态。
/// 
按下公共布尔按钮1
{
得到;
设置
}
/// 
///获取或设置按钮2的状态。
/// 
按下公共布尔按钮2
{
得到;
设置
}
/// 
///获取或设置按钮3的状态。
/// 
公共布尔按钮3按下
{
得到;
设置
}
/// 
///获取或设置按钮4的状态。
/// 
公共布尔按钮4按下
{
得到;
设置
}
/// 
///获取或设置按钮5的状态。
/// 
公共门按钮5按下
{
得到;
设置
}
/// 
///获取或设置按钮6的状态。
/// 
按下公共bool按钮6
{
得到;
设置
}
我想使用内部结果为true的属性将其放入哈希表并转换为字符串。 我尝试的是:

 /// <summary>
        /// Handler listening on Conontroller variables needed to calculate the expression.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">An object that contains no event data.</param>
        public void ConontrollerValuesUpdate(object sender, EventArgs e)
        {
            ControllerValuesArgs conontrollerValuesArgs = new ControllerValuesArgs();

            hashtable["UserInput"] = conontrollerValuesArgs.ToString();
            CalculateExpression();
        }
//
///处理程序侦听计算表达式所需的ConController变量。
/// 
///事件的来源。
///不包含事件数据的对象。
public void concontrollervaluesupdate(对象发送方,事件参数e)
{
ControllerValuesArgs concontrollervaluesargs=新的ControllerValuesArgs();
hashtable[“UserInput”]=concontrollerValuesArgs.ToString();
CalculateExpression();
}

如何在该类的所有属性中调用或搜索真实结果并将其放入表中?

这类似于在ExpandObject实现IDictionary时将任何对象转换为ExpandoObject

这将为您提供具有属性的字典

public static class DynamicExtensions
{
   public static IDictionary<string, object> ToDynamicDictionary(this object value)
    {
        IDictionary<string, object> expando = new ExpandoObject();

        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
            expando.Add(property.Name, property.GetValue(value));

        return expando;
    }
}
公共静态类动态扩展
{
公共静态IDictionary ToDynamicDictionary(此对象值)
{
IDictionary expando=新的ExpandoObject();
foreach(TypeDescriptor.GetProperties(value.GetType())中的PropertyDescriptor属性)
expando.Add(property.Name,property.GetValue(value));
返回expando;
}
}

答案基于

这类似于在ExpandObject实现IDictionary时将任何对象转换为ExpandooObject

这将为您提供具有属性的字典

public static class DynamicExtensions
{
   public static IDictionary<string, object> ToDynamicDictionary(this object value)
    {
        IDictionary<string, object> expando = new ExpandoObject();

        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
            expando.Add(property.Name, property.GetValue(value));

        return expando;
    }
}
公共静态类动态扩展
{
公共静态IDictionary ToDynamicDictionary(此对象值)
{
IDictionary expando=新的ExpandoObject();
foreach(TypeDescriptor.GetProperties(value.GetType())中的PropertyDescriptor属性)
expando.Add(property.Name,property.GetValue(value));
返回expando;
}
}

基于

的回答当你问自己这样的事情时,总是使用集合。例如,按下
bool[]按钮
当你问自己这样的事情时,总是使用集合。例如
bool[]ButtonsPressed
现在,我如何才能只取出带有真正标志形式“expando”的按钮?您必须遍历所有字典项,并检查键与您的姓名标准和值==true匹配的项。这是基本的编程技能。现在,我如何才能只取出带有真正标志形式“expando”的按钮?您将看到要遍历所有字典项,并检查键与您的姓名标准和值==true匹配的项。这是基本的编程技能。