我在Windows窗体应用程序中使用WCF服务。我的WCF服务有两个名为FulltimeEmployee
和ParttimeEmployee
的类。我想通过使用基于员工类型的Windows Fomr应用程序的Wcf服务将用户注册到Sql数据库。用户类型将来自emu类型,但我无法编译它并显示以下错误...
Non-invocable member 'EmployeeType.FullTimeEmployee' cannot be used like a method. HalifaxWindowsFormsApplication C:\Users\Khundokar Nirjor\Documents\Visual Studio 2015\Projects\HalifaxWindowsFormsApplication\HalifaxWindowsFormsApplication\ADDEMPLOYEE.cs 38 Active
如屏幕截图所示。
这是FullTime和PartTime Employee Class的代码..
[DataContract]
public class FullTimeEmployee : Employee
{
public int AnnualSalary { get; set; }
}
[DataContract]
public class PartTimeEmployee : Employee
{
public int HourlyPay { get; set; }
public int HoursWorked { get; set; }
}
这是代码表类..
[KnownType(typeof(FullTimeEmployee))]
[KnownType(typeof(PartTimeEmployee))]
[DataContract(Namespace = "http://pragimtech.com/Employee")]
public class Employee
{
private int _id;
private string _name;
private string _gender;
private DateTime _dateOfBirth;
[DataMember(Order = 1)]
public int Id
{
get { return _id; }
set { _id = value; }
}
[DataMember(Order = 2)]
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember(Order = 3)]
public string Gender
{
get { return _gender; }
set { _gender = value; }
}
[DataMember(Order = 4)]
public DateTime DateOfBirth
{
get { return _dateOfBirth; }
set { _dateOfBirth = value; }
}
[DataMember(Order = 5)]
public EmployeeType Type { get; set; }
}
public enum EmployeeType
{
FullTimeEmployee = 1,
PartTimeEmployee = 2
}
}
以下是根据类型注册员工的WCF服务代码:
public void SaveEmployee(Employee employee)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spSaveEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameterId = new SqlParameter
{
ParameterName = "@Id",
Value = employee.Id
};
cmd.Parameters.Add(parameterId);
SqlParameter parameterName = new SqlParameter
{
ParameterName = "@Name",
Value = employee.Name
};
cmd.Parameters.Add(parameterName);
SqlParameter parameterGender = new SqlParameter
{
ParameterName = "@Gender",
Value = employee.Gender
};
cmd.Parameters.Add(parameterGender);
SqlParameter parameterDateOfBirth = new SqlParameter
{
ParameterName = "@DateOfBirth",
Value = employee.DateOfBirth
};
cmd.Parameters.Add(parameterDateOfBirth);
SqlParameter parameterEmployeeType = new SqlParameter
{
ParameterName = "@EmployeeType",
Value = employee.Type
};
cmd.Parameters.Add(parameterEmployeeType);
if (employee.GetType() == typeof(FullTimeEmployee))
{
SqlParameter parameterAnnualSalary = new SqlParameter
{
ParameterName = "@AnnualSalary",
Value = ((FullTimeEmployee)employee).AnnualSalary
};
cmd.Parameters.Add(parameterAnnualSalary);
}
else
{
SqlParameter parameterHourlyPay = new SqlParameter
{
ParameterName = "@HourlyPay",
Value = ((PartTimeEmployee)employee).HourlyPay,
};
cmd.Parameters.Add(parameterHourlyPay);
SqlParameter parameterHoursWorked = new SqlParameter
{
ParameterName = "@HoursWorked",
Value = ((PartTimeEmployee)employee).HoursWorked
};
cmd.Parameters.Add(parameterHoursWorked);
}
con.Open();
cmd.ExecuteNonQuery();
}
}
以下是Windows窗体应用程序代码:
private void button1_Click(object sender, EventArgs e)
{
MyService.HalifaxServiceClient myservice = new MyService.HalifaxServiceClient("NetTcpBinding_IHalifaxService");
MyService.Employee employee = null;
if (((MyService.EmployeeType)Convert.ToInt32(comboBox2.SelectedValue)) == MyService.EmployeeType.FullTimeEmployee)
{
employee = new MyService.FullTimeEmployee
{
Id = Convert.ToInt32(textBox1.Text),
Name = txtName.Text,
Gender = comboBox1.Text,
DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text),
Type = MyService.EmployeeType.FullTimeEmployee(comboBox2.SelectedValue),//Error
AnnualSalary = Convert.ToInt32(txtAnnualSalary.Text),
};
myservice.SaveEmployee(employee);
label8.Text = "Employee saved";
}
else if (((MyService.EmployeeType)Convert.ToInt32(comboBox2.SelectedValue)) == MyService.EmployeeType.PartTimeEmployee)
{
employee = new MyService.PartTimeEmployee
{
Id = Convert.ToInt32(textBox1.Text),
Name = txtName.Text,
Gender = comboBox1.Text,
DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text),
Type = MyService.EmployeeType.PartTimeEmployee(comboBox2.SelectedValue),//Error
HourlyPay = Convert.ToInt32(txtHourlyPay.Text),
HoursWorked = Convert.ToInt32(txtHoursWorked.Text),
};
myservice.SaveEmployee(employee);
label8.Text = "Employee saved";
}
else
{
label8.Text = "Please select Employee Type";
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.Text == "FullTimeEmployee")
{
txtHourlyPay.Visible = false;
txtHoursWorked.Visible = false;
label6.Visible = false;
label7.Visible = false;
}
else if (comboBox2.Text == "PartTimeEmployee")
{
txtHourlyPay.Visible = true;
txtHoursWorked.Visible = true;
label6.Visible = true;
label7.Visible = true;
}
}
当我点击提交按钮时,它会显示错误消息
并且不会将新员工插入SQL Server数据库..
以下是输出的截图:
请帮我纠正这个错误...
答案 0 :(得分:1)
您需要使用[DataContract]属性标记枚举,并使用[EnumMember]属性标记每个枚举值。我还注意到你已经将你的枚举成员作为员工数据合同的知识类型,而不是枚举本身。
[DataContract]
public class Car
{
[DataMember]
public string model;
[DataMember]
public CarConditionEnum condition;
}
[DataContract(Name = "CarCondition")]
public enum CarConditionEnum
{
[EnumMember]
New,
[EnumMember]
Used,
[EnumMember]
Rental,
Broken,
Stolen
}
有关详情,请查看此link
修改: Here's您正在寻找的内容。你应该尝试类似的东西:
Type = (MyService.EmployeeType) Enum.Parse(typeof(MyService.EmployeeType), comboBox2.SelectedText);