【问题标题】:C# class library, access object propertiesC# 类库,访问对象属性
【发布时间】:2017-08-03 10:20:01
【问题描述】:

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

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;
    };
}

【问题讨论】:

  • 你必须像这样 (MyType) student 将类型对象转换为你的 Student 类型
  • 是的,谢谢,你们都睁大了眼睛,因为错过了参考,我太固执了,没有必要为此投反对票,我是初学者,我的问题很清楚

标签: c# class-library


【解决方案1】:

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

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

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

public bool InsertStudent(Student student)

【讨论】:

  • 您仍然可以使用对象,只需在使用前将其转换为学生
  • 是的,应该使用 DAL.Models.Student 作为类型(谢谢)
  • @VinodSrivastav 你是对的。这是可以做到的。但是,我认为没有任何理由遵循这种方式。通常强制转换是不好的做法,因为它就像争辩说你比编译器更聪明,编译器的工作之一就是这个。当然,在某些情况下这是不可避免的,我们应该使用它,但在这种情况下,我看不出有任何理由。例如,假设您有对此方法的引用,该方法不能转换为 Student 对象。这将结束抛出异常。通过明确说明您应该传递的对象类型是 Student,您可以从一开始就避免这种情况。
  • @Christos:这是有道理的,因为方法本身被称为“InsertStudent”
【解决方案2】:

参数“学生”具有“对象”类型。这是所有类和结构的基本类型。您应该将“学生”转换为您的班级类型

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

或者改变'student'参数类型

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

【讨论】:

    【解决方案3】:

    使用对象类的学生类

    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){ ... }
    

    【讨论】:

      【解决方案4】:

      正如 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;
          };
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        • 2011-02-22
        • 2021-03-05
        • 2016-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多