2017-03-13 125 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; 
    }; 
}