【问题标题】:converting string to?将字符串转换为?
【发布时间】:2013-02-11 21:20:53
【问题描述】:

我正在构建一个动态计算框架,它将基于这些类型的计算类型和属性构建一个通用 GUI。

例如,我可能有一个简单的加法器计算器(请原谅它的简单性,但在组合框架时,我喜欢从简单的开始,然后逐步向上),它看起来像这样:

[CalculatorAttribute("This calculator add X+Y=Z")]
class AdderCalculator : CalculatorBase
{
    [CalculationValueAttribute(InputOutputEnum.InputValue, "First Input")]
    public int? X 
    {
        set { m_X = value; m_Z = null;}
        get { return m_X; }
    }

    [CalculationValueAttribute(InputOutputEnum.InputValue, "Second Input")]
    public int? Y 
    {
        set{ m_Y = value; m_Z = null;}
        get { return m_Y; }
    }

    [CalculationValueAttribute(InputOutputEnum.OutputValue, "Output")]
    public int? Z 
    {
        get { return m_Z; }
    }

    public AdderCalculator()
    {
        m_X = m_Y = m_Z = null;
    }

    public override Boolean ReadyToCalc()
    {
        return (m_X != null && m_Y != null);
    }

    public override Boolean NeedToCalc()
    {
        return (m_Z == null);
    }

    public override void Calculate()
    {
        if(ReadyToCalc()){ m_Z = m_X + m_Y;}
    }

    private int? m_X, m_Y, m_Z;
}

(请原谅空白,我尝试在不牺牲太多可读性的情况下尽可能减小大小)。

我使用可空类型来区分未设置的值和设置的值

现在,在我的 GUI 中,我使用 TextBoxes 来收集输入信息。从TextBox,我只能得到文本(字符串)。使用反射,我知道属性的名称并且可以得到它的设置器(set_Xset_Y 在这种情况下)。

字段的类型(当然)是int?(可为空的int),但我可以使用:

 PropertyInfo.PropertyType.GetGenericArguments();

获取Int32 类型(或Int64,视情况而定)的方法。

所以,鉴于我最终可以得到一个 Type 对象的实例,任何人都可以推荐一种将 String 转换为该类型的通用方法

我考虑的是:

  1. 在我的 AdderCalculator 中实现一个字符串设置方法:set_X(String s)
  2. 将我的控件类型更改为NumericUpDown,因为它将返回Decimal
  3. 将类型更改为Decimal,但Decimal 也不能直接从String 转换(它可以使用解析,但从通用的角度来看这更难

谁能提供更多见解?

【问题讨论】:

  • 看看 [this question][1] 的任何答案是否有帮助? [1]:stackoverflow.com/questions/312858/…
  • @nikeaa - 谢谢,看起来像'object o = Convert.ChangeType(c.Text, ti);'将满足我的需要。仍然需要小心并处理异常。我发誓我搜索了一个小时如何做到这一点,我想我的搜索范围太窄了......感谢指针。

标签: c# winforms reflection


【解决方案1】:

只是为了在可以清楚看到的地方找到答案,我最终得到的代码是:

            // collect relevant inputs
            foreach (Control c in fPanelIn.Controls)
            {
                if (c.Tag is PropertyInfo) 
                {
                    PropertyInfo pi = c.Tag as PropertyInfo;

                    if(c.Text.Length>0)
                    {
                        Type ti = pi.PropertyType;

                        if (ti.IsGenericType)
                        {
                            ti = ti.GetGenericArguments()[0];
                        }
                        object o = Convert.ChangeType(c.Text, ti);

                        pi.SetValue(Calculator, o, null);

                        //MethodInfo mi = calcType.GetMethod(ConstructMethodName(pi), new Type[] { typeof(String) });

                        //mi.Invoke(Calculator, new object[] { c.Text });
                    }
                    else
                    {
                        pi.SetValue(Calculator, null, null);
                    }
                }
            }

我仍然需要为无效值添加一些异常保护,但这适用于任何数字类型的属性类型(byte、short、int、int32、int64、float、double、Decimal 等......)

【讨论】:

    猜你喜欢
    • 2019-10-10
    • 2019-01-05
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2011-08-22
    相关资源
    最近更新 更多