C# 返回C中的任何类型#
我有一个场景,我必须根据条件返回不同类型的对象 为此,我在c#4.0中使用了C# 返回C中的任何类型#,c#,.net,dynamic,ado.net,C#,.net,Dynamic,Ado.net,我有一个场景,我必须根据条件返回不同类型的对象 为此,我在c#4.0中使用了动态返回类型 但我没能做到 public dynamic ValidateUser(string UserName, string Password) { string Result = string.Empty; Employees clsEmployee = new Employees(); Customer clsCustomer = new Customer(); sqlCon
动态返回类型
但我没能做到
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???
}
当我试图回到状态时,它会把我抛在身后
并非所有代码路径返回值都有“错误”
有什么解决方案吗?只需删除其他部分
即可:
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;
}
我认为这将消除当前的问题。为什么不将身份验证从总体中拆分为一个单独的类,或者至少返回IPerson
dynamic
几乎从来都不是正确的解决方案。与其使用最后一个else
语句,不如使用返回结果代码>我强烈怀疑这不是您的实际代码,否则您就不会收到错误消息。我怀疑你最后的else
实际上是一个else if
或类似的东西。正如Tim所建议的,我强烈建议使用Interface或某个父类,可能是一个用户类型的类,后来被继承为customer或Employee,并且总是返回用户类对象。如果结果是意外的,您真的想只返回一个字符串吗?我觉得这很奇怪。和蒂姆一样,我也质疑这里的设计。
switch (Result)
{
case "Employee":
{
...
return ...
}
case "Customer":
{
...
return ....
}
default:
return Result;
}