C# class 库,访问对象属性

C# class library, access object properties

我有一个函数,假设接收一个对象作为参数并使用其属性以供进一步使用,但是当我尝试访问对象属性时,它将它们标记为红色(无法解析符号...) 我试图添加一些参考文献,但没有帮助。

public bool InsertStudent(object student)
{
    var db = DAL.Services.SqlServicesPool.HackService;
    using (var connection = db.StartConnection())
    {
        var sqlParam = new SqlParameter[]
        {
            new SqlParameter("@Name", student.Name),
            new SqlParameter("@Lname", student.Lname),
            new SqlParameter("@Phone", student.Phone),
            new SqlParameter("@Email", student.Email),
            new SqlParameter("@Img", student.Img),
            new SqlParameter("@CityId", student.CityId)
        };
        var result = db.Exec(connection, "spInsertNewStudent", sqlParam);
        db.StopConnection(connection);
         return result;
    };
}

学生的类型是object,或者更正式地说是System.Object。此类型没有任何 属性 称为 NameLname 等。这就是问题所在。您应该将类​​型更改为用于创建学生对象的 class。例如,如果您创建了如下所示的 Student 对象:

var student = new Student
{
    Name = "foo"
    // .....
}

您应该像下面这样更改方法的签名:

public bool InsertStudent(Student student)

参数 'student' 具有 'Object' 类型。这是所有 classes 和结构的基本类型。您应该将 'student' 转换为您的 class 类型

public bool InsertStudent(object obj)
{
    var student = (Student)obj;
    ....
}

或更改'student'参数类型

public bool InsertStudent(Student student)
{
    ....
}

使用学生 class 而不是对象 class

public bool InsertStudent(Student student)
{
    var db = DAL.Services.SqlServicesPool.HackService;
    using (var connection = db.StartConnection())
    {
        var sqlParam = new SqlParameter[]
      {
        new SqlParameter("@Name", student.Name),
        new SqlParameter("@Lname", student.Lname),
        new SqlParameter("@Phone", student.Phone),
        new SqlParameter("@Email", student.Email),
        new SqlParameter("@Img", student.Img),
        new SqlParameter("@CityId", student.CityId)
      };
      var result = db.Exec(connection, "spInsertNewStudent", sqlParam);
      db.StopConnection(connection);
      return result;
   };
}

public class Student
{
   public string Name { get; set; }
   public string Lname { get; set; }
   public string Phone { get; set; }
   public string Email { get; set; }
   public byte []Img { get; set; }
   public string CityId { get; set; }
}

public bool InsertStudent((Student)object student){ ... }

正如 Christos 评论的那样,您需要使用 Student 对象。

public class Student
{
    public string Name { get; set; }
    public string Lname { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Image { get; set; } //Wasn't sure what type you use here
    public int CityId { get; set; }
}

然后传递一个 Student 对象作为您的参数。

public bool InsertStudent(Student student)
{
    var db = DAL.Services.SqlServicesPool.HackService;
    using (var connection = db.StartConnection())
    {
        var sqlParam = new SqlParameter[]
        {
            new SqlParameter("@Name", student.Name),
            new SqlParameter("@Lname", student.Lname),
            new SqlParameter("@Phone", student.Phone),
            new SqlParameter("@Email", student.Email),
            new SqlParameter("@Img", student.Img),
            new SqlParameter("@CityId", student.CityId)
        };
        var result = db.Exec(connection, "spInsertNewStudent", sqlParam);
        db.StopConnection(connection);
        return result;
    };
}