常见模式,这里所以我试图简化它以减少重复

时间:2017-03-22 20:10:31

标签: c#

如果以下方法中的条件具有相似的模式,则所有这些都是 有没有提出一种减少这种方法重复的常用方法的想法?

    public override Func<JObject, dynamic, string> version => (jobject, parameters) =>
    {
        bool hasValidObject = false;

        if (jobject["Version1"] != null)
        {
            _radio.Version1 = new VersionInfo(jobject["Version1"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version2"] != null)
        {
            _radio.Version2 = new VersionInfo(jobject["Version2"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version3"] != null)
        {
            _radio.Version3 = new VersionInfo(jobject["Version3"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version4"] != null)
        {
            _radio.Version4 = new VersionInfo(jobject["Version4"].Value<string>());
            hasValidObject = true;
        }

        if (jobject["Version6"] != null)
        {
            _radio.Version6 = new VersionInfo(jobject["Version6"].Value<string>());
            hasValidObject = true;
        }

        if (hasValidObject)
        {
            return GenerateSuccess();
        }

        return GenerateUnsuccessful( try again.");
    };

3 个答案:

答案 0 :(得分:2)

一种方法是使用反射和循环

public override Func version => (jobject, parameters) => 
{ 
    bool hasValidObject = false;
    for (int i = 1; i<7;i++) 
    {
        hasValidObject = this.SetVersionInfo(i) || hasValidObject;
    }

    if (hasValidObject)
    {
        return GenerateSuccess();
    }

    return GenerateUnsuccessful( "try again.");
};


private bool SetVersionInfo(int i)
{
    if (jobject["Version" + i] == null) return false;

    _radio.GetType().GetProperty(propName)
        .SetValue(_radio, new VersionInfo(jobject["Version" + i].Value<string>()));
    return true;
}

另一种方法是在Dictionary<int,VersionInfo> Versions课程中创建_radio,然后您不需要反思:

private bool SetVersionInfo(int i)
{
    if (jobject["Version" + i] == null) return false;

    _radio.Versions[i] = new VersionInfo(jobject["Version" + i].Value<string>());
    return true;
}

答案 1 :(得分:1)

另一种选择可以是switch语句:

public override Func<JObject, dynamic, string> version => (jobject, parameters) =>
{
    bool hasValidObject = false;

    foreach (char n in "12346")
    {
        var jObj = jobject["Version" + n];
        if (jObj != null)
        {
            var versionInfo = new VersionInfo(jObj.Value<string>());
            switch (n)
            {
                case '1': _radio.Version1 = versionInfo; break;
                case '2': _radio.Version2 = versionInfo; break;
                case '3': _radio.Version3 = versionInfo; break;
                case '4': _radio.Version4 = versionInfo; break;
                case '6': _radio.Version6 = versionInfo; break;
            }
            hasValidObject = true;
        }
    }

    return hasValidObject ? GenerateSuccess() : GenerateUnsuccessful(" try again.");
};

或带有委托数组的更高级版本..因为我喜欢垂直空间:](也未经过测试):

public override Func<JObject, dynamic, string> version => (jobject, parameters) =>
{
    Func<VersionInfo, VersionInfo>[] a = { null, _radio.Version1 = v, 
                v => _radio.Version2 = v, v => _radio.Version3 = v,
                v => _radio.Version4 = v, null, v => _radio.Version6 = v };

    var q = from n in new[] { 1, 2, 3, 4, 6 }
            let j = jobject["Version" + n] where j != null
            select a[n](new VersionInfo(j.Value<string>()));

    return q.Count() > 0 ? GenerateSuccess() : GenerateUnsuccessful(" try again.");
};

答案 2 :(得分:0)

如果 jobject 是Dictionary类型,您可以使用

运行它
foreach(string version in jobject.keys)
{
     if(jobject[version]!=null){
     //do something
     }
}

问题在于收音机,但如果是你的班级,你可以在班级中使用一个版本的数组而不是那么多,并使用计数器来访问它们,你将使用它来计算循环的数量。编号