简体   繁体   English

如何将这些代码更改为C#.net framework 3.5?

[英]How can I change these codes to C#.net framework 3.5?

Here is the code of .net framework 4.0. 这是.net Framework 4.0的代码。 But our old system is using 3.5. 但是我们的旧系统使用的是3.5。 It's difficult to upgrade all the codes. 升级所有代码很困难。 I want to know how to change the codes written by 4.0 to 3.5 codes. 我想知道如何将由4.0编写的代码更改为3.5代码。

The main problem is I don't know how to convert "return string.Join(",", states);" 主要问题是我不知道如何转换“ return string.Join(“,”,状态);“ Error happened when I was trying to compile it using .net framework 3.5. 当我尝试使用.net framework 3.5进行编译时发生错误。

Thank you! 谢谢!

public enum States
{
....
}

public static string GetStates(uint stateFlags)
{
    var stateList = Enum.GetValues(typeof(States));
    var states = default(States);

    foreach (var state in stateList)
    {
        if (state == null) continue;
        var stateEnum = (States)state;
        if (HasState(stateFlags, stateEnum))
        {
            states = states | stateEnum;
        }
    }

    return string.Join(",", states);
}

The error is Error The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments 错误为错误。'string.Join(string,string [])'的最佳重载方法匹配具有一些无效的参数

string.Join in .Net 3.5 only supports a string array whereas .Net 4.0 has additional overloads to work with IEnumerable<string> or Object[] . .Net 3.5中的string.Join仅支持字符串数组,而.Net 4.0具有其他重载以与IEnumerable<string>Object[]

You should be passing a string[] to the Join method. 您应该将一个string[]传递给Join方法。

More at the MSDN docs: 有关MSDN文档的更多信息:

.Net 4 : http://msdn.microsoft.com/en-us/library/dd992421(v=vs.100).aspx .Net 4: http//msdn.microsoft.com/en-us/library/dd992421( v = vs.100).aspx

.Net 3.5: http://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.90).aspx .Net 3.5: http//msdn.microsoft.com/en-us/library/57a79xd0( v = vs.90).aspx

In .Net 3.5 the overload you used for "String.Join" is not available. 在.Net 3.5中,用于“ String.Join”的重载不可用。 Replace your return row with the following 2 rows: 将您的返回行替换为以下两行:

 string[] stateNames = Enum.GetNames(typeof(States));
 return string.Join(",", stateNames);

The expected second argument is an array of Strings. 预期的第二个参数是字符串数组。

Here is a slightly modified version of your original solution 这是原始解决方案的略微修改版本

public enum States
{
    None = 0,
    StateOne = 1,
    StateTwo = 2,
    StateThree = 4,
    StateFour = 8,
};

public static string GetStates(uint stateFlags)
{
    var stateList = Enum.GetValues(typeof(States));
    List<States> states = new List<States>();

    foreach (int state in stateList)
    {        
        if ((stateFlags & state) != 0)
        {
            states.Add((States)state);
        }
    }

    return string.Join(",", states);
}

Running 跑步

GetStates((uint)(States.StateOne | States.StateTwo))

outputs 输出

StateOne,StateTwo

Thank you everybody for your warmly help. 谢谢大家的热烈帮助。 I changed the last line to below: 我将最后一行更改为以下内容:

        var valuesAsList = GetValues(states).Cast<string>().ToList();

        return string.Join(",", valuesAsList.ToArray());

And added the method: 并添加了方法:

    public static IEnumerable<Enum> GetValues(Enum enumeration)
    {
        List<Enum> enumerations = new List<Enum>();
        foreach (FieldInfo fieldInfo in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
        {
            enumerations.Add((Enum)fieldInfo.GetValue(enumeration));
        }
        return enumerations;
    }

The compile error disappeared, but I still haven't test the code to see whether it's the same result with .net 4.0. 编译错误消失了,但是我仍然没有测试代码以查看与.net 4.0是否相同。

Thanks again. 再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM