我有一个场景,我必须根据条件返回不同类型的对象。
为此,我在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???
}
当我试图在条件内返回时,它会抛出我
并非所有代码路径都返回值'错误。
任何解决方案?
答案 0 :(得分:1)
switch (Result)
{
case "Employee":
{
...
return ...
}
case "Customer":
{
...
return ....
}
default:
return Result;
}
答案 1 :(得分:0)
只需删除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;
答案 2 :(得分:0)
Santhosh,
这不是处理不同类型的正确方法。您可以考虑实现这个接口的实现。这使得可读性更高,更具可扩展性。我相信您正在制定支持代码,因此您可能无法实现新的实施。
我认为你可以像这样改变
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;
}
我认为这将消除当前的问题。