繁体   English   中英

在C#中返回任何类型

[英]Return any Type in C#

我有一种情况,我必须根据条件返回不同类型的对象。

为此,我在c#4.0中使用了dynamic返回类型。

但是我做不到。

public dynamic ValidateUser(string UserName, string Password)
{
    string Result = string.Empty;

    Employees clsEmployee = new Employees();
    Customer clsCustomer = new Customer();

    sqlConnection = new SqlConnection(Connection());
    command = new SqlCommand("dbo.usp_ValidateUser", sqlConnection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@Username", SqlDbType.VarChar).Value = UserName;
    command.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
    sqlConnection.Open();

    SqlParameter newSqlParam = new SqlParameter();
    newSqlParam.ParameterName = "@Result";
    newSqlParam.SqlDbType = SqlDbType.NVarChar;
    newSqlParam.Direction = ParameterDirection.Output;
    newSqlParam.Size = 50;
    command.Parameters.Add(newSqlParam);

    SqlDataReader dr = command.ExecuteReader();
    Result = command.Parameters["@Result"].Value.ToString();

    if (Result == "Employee")
    {
        while (dr.Read())
        {
            clsEmployee.EmployeeId = (int)dr["EmployeeId"];
            clsEmployee.EmployeeName = (string)dr["EmployeeName"];
            clsEmployee.DepartmentName = (string)dr["DepartmentName"];
            clsEmployee.RoleName = (string)dr["RoleName"];
        }    
        return clsEmployee;
    }
    else if (Result == "Customer")
    {
        while (dr.Read())
        {
            clsCustomer.CustomerId = (int)dr["CustomerId"];
            clsCustomer.CustomerName = (string)dr["CustomerName"];
            clsCustomer.CustomerEmail = (string)dr["CustomerEmail"];
            clsCustomer.CustomerMobile = (string)dr["CustomerMobile"];
        }
        return clsCustomer;
    }


    //How to return???
}

当我尝试返回病情时,它会抛出我

并非所有代码路径返回值都出错。

有什么办法吗?

switch (Result)
{
    case "Employee":
    {
        ...
        return ...
    }
    case "Customer":
    {
        ...
        return ....
    }
    default:
        return Result;
}

只需删除else部分:

else if (Result == "Customer")
{
    while (dr.Read())
    {
        clsCustomer.CustomerId = (int)dr["CustomerId"];
        clsCustomer.CustomerName = (string)dr["CustomerName"];
        clsCustomer.CustomerEmail = (string)dr["CustomerEmail"];
        clsCustomer.CustomerMobile = (string)dr["CustomerMobile"];
    }
    return clsCustomer;
}
return Result;

桑索什

这不是处理不同类型的正确方法。 您可以考虑实现此目的的接口的实现。 这样可以提高可读性和扩展性。 我相信您正在开发支持代码,因此您可能在实现新实现方面受到限制。

我想你可以这样改变

public object ValidateUser(string UserName, string Password)
{
    object retVal= null;
    string Result = String.Empty;

    Employees clsEmployee = new Employees();
    Customer clsCustomer = new Customer();

    // get the data
    Result = command.Parameters["@Result"].Value.ToString();

    if (Result == "Employee")
    {
        while (dr.Read())
        {
            clsEmployee.EmployeeId = (int)dr["EmployeeId"];
            clsEmployee.EmployeeName = (string)dr["EmployeeName"];
            clsEmployee.DepartmentName = (string)dr["DepartmentName"];
            clsEmployee.RoleName = (string)dr["RoleName"];
        }

        retVal=clsEmployee;
    }
   if (Result == "Customer")
    {
        while (dr.Read())
        {
            clsCustomer.CustomerId = (int)dr["CustomerId"];
            clsCustomer.CustomerName = (string)dr["CustomerName"];
            clsCustomer.CustomerEmail = (string)dr["CustomerEmail"];
            clsCustomer.CustomerMobile = (string)dr["CustomerMobile"];
        }

        retVal=clsCustomer;
    }
     retVal=Result;
     return retVal;
    }

我认为这将消除当前的问题。

暂无
暂无

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

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