2
我需要創建對象的一個列表,其中的對象是從一個字符串.NET創建對象的列表,其中對象類型是從一個字符串
例如我想下面的方式轉換爲對象
Dim categoryList as IList(Of "classname") = new List(Of "class name")
最終結果的列表
Dim categoryList As IList(Of BO.Store.Category) = New List(Of BO.Store.Category)
感謝xxbbcc指着我在正確的方向。我想出了下面的解決方案,它可以讓我上傳任何csv文件並將其解析爲x對象列表。
這可以通過使用dropzone上傳文件以及表名,對象類名和映射類名來實現。這現在允許我使用csvhelper解析文件的內容,準備導入臨時表。
<AcceptVerbs(HttpVerbs.Post), BaseViewModelFilter>
Function Upload(model As ImportUploadViewModel) As JsonResult
Dim balUpload As BAL.Upload = New BAL.Upload()
Dim balImport As BAL.Import = New BAL.Import()
Dim folder As String = String.Format("{0}tmp\import\category", Server.MapPath("\"))
Dim result = balUpload.SaveFiles(Request, folder, UserProfile.ID)
Dim importClassType = Type.[GetType](String.Format("Roxybox.BO.{0}", model.EntityClass))
Dim importClassMapType = Type.[GetType](String.Format("Roxybox.BO.{0}", model.EntityMap))
Dim records As IList
' Import all successful files
For Each successFile As String In result("success")
' Parse csv file
Using sr = New StreamReader(String.Format("{0}\{1}", folder, successFile))
Dim reader = New CsvReader(sr)
reader.Configuration.RegisterClassMap(importClassMapType)
records = reader.GetRecords(importClassType).ToList()
End Using
For Each category In records
Dim data As BO.Import = New BO.Import()
With data
.EntityModel = model.EntityModel
.Data = JsonConvert.SerializeObject(category)
.UserProfileID = UserProfile.ID
.Filename = successFile
.Status = "pending"
End With
balImport.Save(data, UserProfile.ID)
Next
Next
Return Json(result, JsonRequestBehavior.AllowGet)
End Function
我將如何使用它來創建對象列表? – adamias
@adamias我將添加一個小樣本來顯示它。你能讀/翻譯C#嗎? (我的C#比我的VB.Net :) :) – xxbbcc
退房http://msdn.microsoft.com/en-us/library/d133hta4(v=vs.110).aspx –