简体   繁体   English

C#如何声明未知的方法类型

[英]C# How to Declare unknown method types

I have to provide search functionality to end user and for that purpose i am providing a dropdown and a textbox, the dropdown has options like EmployeeID, DisplayName, City and after selecting the dropdown value he needs to enter a value in the textbox. 我必须为最终用户提供搜索功能,为此我提供了一个下拉菜单和一个文本框,下拉列表中有EmployeeID,DisplayName,City等选项,在选择下拉值后,他需要在文本框中输入一个值。

If the user selects EmployeeID then the value will be of type Int and if he selects Name or city the value will be of type string. 如果用户选择EmployeeID,则该值将为Int类型,如果他选择Name或city,则该值将为string类型。

Public Employee getEmpInfo(object querystring, object queryvalue)
{
// code to get the empplyee info
}

I would like to know if there is any better approach than using object type ? 我想知道是否有比使用对象类型更好的方法?

You can overload your method like this: 您可以像这样重载您的方法:

If your parameter is int: 如果您的参数是int:

public Employee getEmpInfo(int employeeId){
   // query by ID
}

Or a string 或者一个字符串

public Employee getEmpInfo(string employeeName){
    // query by name
}

Then if you give string, the second will be used by the program, else the first will be. 然后,如果你给字符串,第二个将由程序使用,否则第一个将。 You can both write these functions in your file. 您可以在文件中编写这些函数。 It's called method overloading . 它被称为方法重载

References: 参考文献:

https://www.geeksforgeeks.org/c-sharp-method-overloading/ https://www.geeksforgeeks.org/c-sharp-method-overloading/

https://www.tutorialspoint.com/What-is-method-overloading-in-Chash https://www.tutorialspoint.com/What-is-method-overloading-in-Chash

Function Overloading 功能重载


On the other hand, if you want to use one function to search all types: 另一方面,如果要使用一个函数来搜索所有类型:

public class SearchParams {
    public int ID;
    public string Name;
    public string City;

    public SearchParams(int ID = null, string Name = null, string City = null)
    { 
         this.ID = ID;
         this.Name = Name;
         this.City = City;    
    }
}


public function getEmpInfo(SearchParams params){
    // build query by that parameters
    if(ID != null){
       // add query by ID
    }
}

It is possible to solve this in the UI by adding and binding controls for all properties and depending on the selected property name in the combobox, showing only the control for selected property. 可以通过为所有属性添加和绑定控件并根据组合框中选定的属性名称在UI中解决此问题,仅显示所选属性的控件。

This approach makes it possible to have very specific controls for some properties instead of forcing everything to string input. 这种方法可以对某些属性进行非常具体的控制,而不是将所有内容强制为字符串输入。

How this should be done depends on the UI technology (Web, WinForms, WPF, ...) 如何做到这一点取决于UI技术(Web,WinForms,WPF,...)

In my opinion your approach is wrong. 在我看来,你的方法是错误的。

If you search an Employee by name or by ID or by date of birth, you execute completely different actions and SQL operations. 如果按名称或ID或出生日期搜索Employee,则执行完全不同的操作和SQL操作。

You should have a version of your method for every possibility, to perform in every method a specific set of operations. 您应该为每种可能性设置一个版本的方法,以便在每个方法中执行一组特定的操作。

If you make a single method based on parameters types in override, you risk to have troubles when you have different columns with the same type. 如果基于覆盖中的参数类型创建单个方法,则当具有相同类型的不同列时,可能会遇到麻烦。


This is wrong (and can't even compile): 这是错误的(甚至无法编译):

public Employee GetEmployee(int id)
{
    //Instructions..
}

public Employee GetEmployee(string name)
{
    //Instructions..
}

public Employee GetEmployee(string city)
{
    //Instructions..
}

This will support every possibility and mantains readability: 这将支持所有可能性并保持可读性:

public Employee GetEmployeeByID(int id)
{
    //Instructions..
}

public Employee GetEmployeeByName(string name)
{
    //Instructions..
}

public Employee GetEmployeeByCity(string city)
{
    //Instructions..
}

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

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