C# 如何重构到更小的函数?

C# 如何重构到更小的函数?,c#,refactoring,C#,Refactoring,我有一个为ASP.NET MVC加载大型选择列表的函数。 此函数的methodsize为354行。 我想重构到更多的函数或一个局部字段,这样每个函数将少于40行 以下是代码片段: public static SelectList CreateShutterSpeedList() { var shutterSpeedList = new List<CameraSettingItem>(); var secNotation

我有一个为ASP.NET MVC加载大型选择列表的函数。 此函数的methodsize为354行。 我想重构到更多的函数或一个局部字段,这样每个函数将少于40行

以下是代码片段:

public static SelectList CreateShutterSpeedList()
        {
            var shutterSpeedList = new List<CameraSettingItem>();

            var secNotationPostfix = "\"";

            shutterSpeedList.Add(new CameraSettingItem
            {
                Id = ShutterSpeedDefaultValue,
                Description = string.Empty
            });

            shutterSpeedList.Add(new CameraSettingItem
            {
                Id = 1,
                Description = "30" + secNotationPostfix
            });

etc
public static SelectList CreateShutterSpeedList()
{
var shutterSpeedList=新列表();
var secNotationPostfix=“\”;
shutterSpeedList.Add(新摄像机设置项)
{
Id=默认值,
Description=string.Empty
});
shutterSpeedList.Add(新摄像机设置项)
{
Id=1,
Description=“30”+secNotationPostfix
});
等

可能是一个私有列表作为变量?或者从文件加载?或者…?

如果按顺序分配
ShutterSpeedDefaultValue
上面的ID,您可以先创建一个描述数组,然后将其转换为带有LINQ的
CameraSettingItem
列表:

var descriptions = new[] {
    string.Empty
,   "30" + secNotationPostfix
,   ...
};
shutterSpeedList = descriptions
    .Select((d,i) => new CameraSettingItem {
        Id = i==0 ? ShutterSpeedDefaultValue : i
    ,   Description = d
    })
    .ToList();
您还可以在方法主体之外创建
CameraSettingItem
s的列表,如下所示:

private const string secNotationPostfix = "\"";

private static IList<CameraSettingItem> shutterSpeedList = new List<CameraSettingItem> {
    new CameraSettingItem {
        Id = ShutterSpeedDefaultValue,
        Description = string.Empty
    },
    new CameraSettingItem {
        Id = 1,
        Description = "30" + secNotationPostfix
    },
    ...
};

public static SelectList CreateShutterSpeedList() {
    return new SelectList(shutterSpeedList, "Id", "Description");
}
private const string secNotationPostfix=“\”;
私有静态IList shutterSpeedList=新列表{
新摄像机设置项{
Id=默认值,
Description=string.Empty
},
新摄像机设置项{
Id=1,
Description=“30”+secNotationPostfix
},
...
};
公共静态SelectList CreateShutterSpeedList(){
返回新的SelectList(shutterSpeedList、“Id”、“描述”);
}

如果按顺序分配了
ShutterSpeedDefaultValue
上面的ID,您可以先创建一个描述数组,然后将其转换为带有LINQ的
CameraSettingItem
列表:

var descriptions = new[] {
    string.Empty
,   "30" + secNotationPostfix
,   ...
};
shutterSpeedList = descriptions
    .Select((d,i) => new CameraSettingItem {
        Id = i==0 ? ShutterSpeedDefaultValue : i
    ,   Description = d
    })
    .ToList();
您还可以在方法主体之外创建
CameraSettingItem
s的列表,如下所示:

private const string secNotationPostfix = "\"";

private static IList<CameraSettingItem> shutterSpeedList = new List<CameraSettingItem> {
    new CameraSettingItem {
        Id = ShutterSpeedDefaultValue,
        Description = string.Empty
    },
    new CameraSettingItem {
        Id = 1,
        Description = "30" + secNotationPostfix
    },
    ...
};

public static SelectList CreateShutterSpeedList() {
    return new SelectList(shutterSpeedList, "Id", "Description");
}
private const string secNotationPostfix=“\”;
私有静态IList shutterSpeedList=新列表{
新摄像机设置项{
Id=默认值,
Description=string.Empty
},
新摄像机设置项{
Id=1,
Description=“30”+secNotationPostfix
},
...
};
公共静态SelectList CreateShutterSpeedList(){
返回新的SelectList(shutterSpeedList、“Id”、“描述”);
}

您可以将需要的项目存储在JSON或XML文件中,并在需要时使用或反序列化它们,例如:

public static SelectList CreateShutterSpeedList(
{
    var json = File.ReadAllText(@"\ShutterSpeedList.json");
    var shutterSpeedList = JsonConvert.DeserializeObject<List<CameraSettingItem>>(json);
    // Convert shutterSpeedList to SelectList and return
}

您可以将需要的项目存储在JSON或XML文件中,并在需要时使用或反序列化它们,例如:

public static SelectList CreateShutterSpeedList(
{
    var json = File.ReadAllText(@"\ShutterSpeedList.json");
    var shutterSpeedList = JsonConvert.DeserializeObject<List<CameraSettingItem>>(json);
    // Convert shutterSpeedList to SelectList and return
}

创建一个用于添加到该列表中的委托,并使用参数调用该委托。我投票将此问题作为非主题结束,因为改进工作代码不属于Stackoverflow,而属于@TimSchmelter和partickMeters:请注意,我们需要完整函数的代码进行代码检查。创建一个用于添加到该列表和cal的委托l带参数的委托人。我投票结束这个问题,因为改进工作代码不属于Stackoverflow,而属于@TimSchmelter和partickMeters:请注意,我们需要完整函数的代码进行代码审查。创建一个委托人,添加到该列表中,并调用带参数的委托人。我投票支持close由于改进工作代码不属于Stackoverflow,而是@TimSchmelter和PartickMeters,因此此问题不属于主题:请注意,我们需要完整函数的代码进行代码检查。Id不是按顺序排列的……我当前有私有数组:public static CameraSettingItem[]cameraSettingItems=new CameraSettingItem[]{new CameraSettingItem{Id=ShutterSpeedDefaultValue,Description=string.Empty},new CameraSettingItem{Id=1,Description=“30”+secNotationPostfix};第二个建议足够好了,私有成员变量作为数组,最后是.ToList();代码从354行到3:-)Id不是按顺序排列的…我当前有一个私有数组:public static CameraSettingItem[]cameraSettingItems=new CameraSettingItem[]{new CameraSettingItem{Id=ShutterSpeedDefaultValue,Description=string.Empty},new CameraSettingItem{Id=1,Description=“30”+secNotationPostfix};第二个建议足够好了,私有成员变量作为数组,最后是.ToList();代码从354行到3:-)Id不是按顺序排列的……我当前有私有数组:public static CameraSettingItem[]cameraSettingItems=new CameraSettingItem[]{new CameraSettingItem{Id=ShutterSpeedDefaultValue,Description=string.Empty},new CameraSettingItem{Id=1,Description=“30”+secNotationPostfix};第二个建议足够好了,私有成员变量作为数组,最后是.ToList();代码从354行到3:-)