以下方法創建 linq-to-sql對象運行時間基於類名稱。如何通過反射動態創建並保存LINQ-to-SQL對象?
我也可以通過反射填充物品的屬性。
但是我如何通過linq-to-sql命令保存這個對象,如果我在運行時不知道它是哪種類型的話?
public void CreateItem(string className)
{
//create instance of item
string classNamespaceAndName = "TestApp.Models." + className;
Type itemType = Type.GetType(classNamespaceAndName);
item = (object)Activator.CreateInstance(itemType, new Object[] { });
//fill properties through reflection
//save item to the database
db.?????.InsertOnSubmit(item as itemType); //error
db.SubmitChanges();
}