C# 参数重载以避免开发人员';他犯了错误,必须遵守习惯规则
这是我的列举C# 参数重载以避免开发人员';他犯了错误,必须遵守习惯规则,c#,C#,这是我的列举 public enum HeaderDisplayType { singleColumn, table, doubleColumn }; 如果开发人员在枚举部分后输入逗号,他可以看到另一个字符串参数“RowName”。 请告诉我如何限制开发人员在不使用枚举“singleColumn”时访问第二种方法。换句话说,允许开发人员在仅使用枚举“单列”时访问第二个方法 通过这种方式,开发
public enum HeaderDisplayType
{
singleColumn,
table,
doubleColumn
};
如果开发人员在枚举部分后输入逗号,他可以看到另一个字符串参数“RowName”。
请告诉我如何限制开发人员在不使用枚举“singleColumn”时访问第二种方法。换句话说,允许开发人员在仅使用枚举“单列”时访问第二个方法
通过这种方式,开发人员将能够遵循某个条件而不会遇到任何麻烦。这取决于模型的业务场景
这应该在不更改方法名称的情况下完成,并且仅根据上述条件定义参数
谢谢你你的问题不清楚,但是,我认为你在这个结构之后:
objectName.crWsheet("heading", intList, ExcelCreator.HeaderDisplayType.singleColumn)
公共工作表crWsheet(字符串主标题,列表)
{
ExcelCreator.HeaderDisplayType headerType=HeaderDisplayType.doubleColumn
}
公共工作表crWsheet(字符串主标题、列表、字符串行名)
{
ExcelCreator.HeaderDisplayType headerType=HeaderDisplayType.singleColumn;
}
因此,现在提供
RowName
隐含地表示您正在使用singleColumn
,而不指定这一点意味着doubleColumn
您的问题不清楚,但我认为您在使用此结构之后:
objectName.crWsheet("heading", intList, ExcelCreator.HeaderDisplayType.singleColumn)
公共工作表crWsheet(字符串主标题,列表)
{
ExcelCreator.HeaderDisplayType headerType=HeaderDisplayType.doubleColumn
}
公共工作表crWsheet(字符串主标题、列表、字符串行名)
{
ExcelCreator.HeaderDisplayType headerType=HeaderDisplayType.singleColumn;
}
因此,现在提供
RowName
隐式表示您正在使用singleColumn
,而不指定这一点意味着doubleColumn
,如果第二种方法只应与HeaderDisplayType.doubleColumn参数值一起使用,则应从中删除headerType参数:
public Worksheet crWsheet<T>(string masterHeading, List<T> list)
{
ExcelCreator.HeaderDisplayType headerType = HeaderDisplayType.doubleColumn
}
public Worksheet crWsheet<T>(string masterHeading, List<T> list, string RowName)
{
ExcelCreator.HeaderDisplayType headerType = HeaderDisplayType.singleColumn;
}
公共工作表crWsheet(字符串主标题、列表列表、ExcelCreator.HeaderDisplayType headerType=HeaderDisplayType.doubleColumn)
{
//方法体
}
公共工作表crWsheet(字符串主标题、列表、字符串RowName=“Row1”)
{
var headerType=HeaderDisplayType.doubleColumn;
//方法体
}
对于其他标题显示类型,您必须使用类而不是枚举:
public Worksheet crWsheet<T>(string masterHeading, List<T> list, ExcelCreator.HeaderDisplayType headerType = HeaderDisplayType.doubleColumn)
{
// method body
}
public Worksheet crWsheet<T>(string masterHeading, List<T> list, string RowName="Row1")
{
var headerType = HeaderDisplayType.doubleColumn;
// method body
}
公共类名称DroWheaderType:RowHeaderType
{
}
公共类RowHeaderType
{
}
公共类标题显示类型
{
公共静态NamedRowHeaderType doubleColumn=新NamedRowHeaderType();
公共静态NamedRowHeaderType表=新NamedRowHeaderType();
公共静态RowHeaderType singleColumn=新的RowHeaderType();
}
类样本
{
公共工作表crWsheet(字符串主标题、列表列表、ExcelCreator.RowHeaderType headerType=null)
{
headerType=headerType??ExcelCreator.HeaderDisplayType.doubleColumn;
//方法体
返回null;
}
公共工作表crWsheet(字符串主标题,列表列表,ExcelCreator.NamedRowHeaderType标题类型=null,字符串RowName=“Row1”)
{
headerType=headerType??ExcelCreator.HeaderDisplayType.doubleColumn;
//方法体
返回null;
}
静态void Main(字符串[]参数)
{
样本s;
s、 crWsheet(“”,null,ExcelCreator.HeaderDisplayType.singleColumn);
s、 crWsheet(“”,null,ExcelCreator.HeaderDisplayType.doubleColumn,“行1”);
s、 crWsheet(“”,null,ExcelCreator.HeaderDisplayType.singleColumn,“行1”);//编译错误
}
}
如果第二种方法应仅与HeaderDisplayType.doubleColumn参数值一起使用,则应从中删除headerType参数:
public Worksheet crWsheet<T>(string masterHeading, List<T> list)
{
ExcelCreator.HeaderDisplayType headerType = HeaderDisplayType.doubleColumn
}
public Worksheet crWsheet<T>(string masterHeading, List<T> list, string RowName)
{
ExcelCreator.HeaderDisplayType headerType = HeaderDisplayType.singleColumn;
}
公共工作表crWsheet(字符串主标题、列表列表、ExcelCreator.HeaderDisplayType headerType=HeaderDisplayType.doubleColumn)
{
//方法体
}
公共工作表crWsheet(字符串主标题、列表、字符串RowName=“Row1”)
{
var headerType=HeaderDisplayType.doubleColumn;
//方法体
}
对于其他标题显示类型,您必须使用类而不是枚举:
public Worksheet crWsheet<T>(string masterHeading, List<T> list, ExcelCreator.HeaderDisplayType headerType = HeaderDisplayType.doubleColumn)
{
// method body
}
public Worksheet crWsheet<T>(string masterHeading, List<T> list, string RowName="Row1")
{
var headerType = HeaderDisplayType.doubleColumn;
// method body
}
公共类名称DroWheaderType:RowHeaderType
{
}
公共类RowHeaderType
{
}
公共类标题显示类型
{
公共静态NamedRowHeaderType doubleColumn=新NamedRowHeaderType();
公共静态NamedRowHeaderType表=新NamedRowHeaderType();
公共静态RowHeaderType singleColumn=新的RowHeaderType();
}
类样本
{
公共工作表crWsheet(字符串主标题、列表列表、ExcelCreator.RowHeaderType headerType=null)
{
headerType=headerType??ExcelCreator.HeaderDisplayType.doubleColumn;
//方法体
返回null;
}
公共工作表crWsheet(字符串主标题,列表列表,ExcelCreator.NamedRowHeaderType标题类型=null,字符串RowName=“Row1”)
{
headerType=headerType??ExcelCreator.HeaderDisplayType.doubleColumn;
//方法体
返回null;
}
静态void Main(字符串[]参数)
{
样本s;
s、 crWsheet(“”,null,ExcelCreator.HeaderDisplayType.singleColumn);
s、 crWsheet(“”,null,ExcelCreator.HeaderDisplayType.doubleColumn,“行1”);
s、 crWsheet(“”,null,ExcelCreator.HeaderDisplayType.singleColumn,“行1”);//编译错误
}
}
无法为枚举值添加编译时限制。当singleColumn传递给第二个方法时,应该抛出一个异常。或者用两个具有静态字段/属性的类替换Enum。为类而不是Enums添加了示例无法为Enum值添加编译时限制。当singleColumn传递给第二个方法时,应该抛出一个异常。或将枚举替换为两个具有静态字段/属性的类。为类而不是枚举添加了示例