Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法通过多个参数命名GetX_C#_Methods_Overloading - Fatal编程技术网

C# 方法通过多个参数命名GetX

C# 方法通过多个参数命名GetX,c#,methods,overloading,C#,Methods,Overloading,我正在设计一个factory类,想知道基于不同参数检索对象集合的可接受的方法命名约定是什么 例如: 从我的数据源获取所有对象(#1无参数): 获取具有匹配名称(#2 name参数)的所有对象: 获取具有匹配“其他属性”(#3“其他属性”参数)的所有对象: 现在,我遇到的麻烦是,当我想添加一个获取两个或更多属性的get方法时: 获取具有匹配名称和“另一个属性”的所有对象(#4两个参数): 我的几次尝试: public GetByNameAndAnotherProperty(string name,

我正在设计一个factory类,想知道基于不同参数检索对象集合的可接受的方法命名约定是什么

例如:

从我的数据源获取所有对象(#1无参数):

获取具有匹配名称(#2 name参数)的所有对象:

获取具有匹配“其他属性”(#3“其他属性”参数)的所有对象:

现在,我遇到的麻烦是,当我想添加一个获取两个或更多属性的get方法时:

获取具有匹配名称和“另一个属性”的所有对象(#4两个参数):

我的几次尝试:

public GetByNameAndAnotherProperty(string name, string anotherProperty){...}
public GetByNameByAnotherProperty(string name, string anotherProperty){...}
public Get(string name, string anotherProperty){...}
我可以看到,当我有两个或三个以上的参数时,前两种方法变得非常麻烦。例如:

public GetByNameByPropertyXByPropertyYByPropertyZ(string name, etc.){...}
有更好的方法吗?如何设计一个灵活的类来接受任意数量的参数,并保持命名约定干净简洁


任何帮助都会很好

也许我误解了这个问题,但在我看来你在寻找这个:


非常感谢。但是我如何区分列表中的项目是属性A还是属性B?你不能。如果必须区分它们,请创建一个表示方法输入参数的类。如果您有5个以上的输入参数,也建议使用此方法。所以,它看起来是这样的:publicFooClass GetByProperties(ParamClass参数);我决定使用可选/命名参数来最小化方法重载的需要。谢谢你的帮助。
public GetByAnotherProperty(string anotherProperty){...}
public GetByNameAndAnotherProperty(string name, string anotherProperty){...}
public GetByNameByAnotherProperty(string name, string anotherProperty){...}
public Get(string name, string anotherProperty){...}
public GetByNameByPropertyXByPropertyYByPropertyZ(string name, etc.){...}
public FooClass GetByProperties(params string[] list)