2017-03-13 127 views
-5

我有想接受一个对象作为参数,并使用其属性为进一步使用功能,但是当我尝试访问对象的属性也将其标记为红色(不能解析符号。 ..) 我试图添加几个参考,但它没有帮助。C#类库,访问对象的属性

enter image description here

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

你要投类型的对象,以你的学生类型像这样(的MyType)学生 – Serghei

+0

[C#投对象其数据类型obj.GetType()]的可能的复制(http://stackoverflow.com/questions/31821151/C锋利投对象到其-数据类型-OBJ-将gettype) – jomsk1e

+0

是的,谢谢,ü所有睁开眼睛的原因,是我太固定丢失的引用后,没有必要下投我要是,我是一个初学者,我的问题是明确的和干净的 – tomersss2

回答

3

类型的学生为object,或者更正式System.Object。这种类型的没有任何财产被称为NameLname等。这就是问题所在。您应该将类​​型更改为用于创建学生对象的类。例如,如果你已经创建像下面的学生对象:

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

你应该改变你的方法的签名如下图所示:

public bool InsertStudent(Student student) 
+0

您仍然可以可以使用对象,使用前 –

+0

是的只是把它转换为学生,应该使用DAL.Models.Student作为类型(感谢的人) – tomersss2

+0

@ tomersss2不用谢 ! – Christos

0

参数“学生”有“对象”型。这是所有类和结构的基本类型。你应该投 '学生' 到你的类类型

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

或更改 '学生' 参数类型

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

使用Student类insted的对象类的

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

正如克里斯托评论说,你需要使用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; 
    }; 
}