我有很多模型类,我用来自 API 的数据填充。
API 数据的结构是 DataRow。
对于每个类,都有从 DataRow 到模型类的隐式转换。我必须为每个模型类编写一组函数。其中一个函数,从数据库中获取,对于所有模型类都是通用的,所以我决定为此编写具有泛型类型的函数。
这是模型类的示例:
public class Person
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public static implicit operator Person(DataRow dataRow)
{
if (dataRow != null)
{
return new Person
{
Id = long.Parse((string)dataRow["Id"]),
FirstName = (string)dataRow["FirstName"],
LastName = (string)dataRow["LastName"]
};
};
return null;
}
}
具有用于强制转换的泛型类型的类示例,如下所示:
public class DefaultRepository<T>
{
public T CreateInstanceOfModelFromDataRow(DataRow dataRow)
{
T modelInstance = (T)Activator.CreateInstance(typeof(T));
modelInstance = (T)dataRow;
return modelInstance;
}
}
测试代码如下所示:
// for testing I will create DataTable and DataRow and populate it fake data
DataTable fakeTable = new DataTable();
fakeTable.Columns.Add("Id");
fakeTable.Columns.Add("FirstName");
fakeTable.Columns.Add("LastName");
DataRow fakeRow = fakeTable.NewRow();
fakeRow["Id"] = "1";
fakeRow["FirstName"] = "John";
fakeRow["LastName"] = "Doe";
// Cast DataRow to Pesron without error
Person fakePerson01 = (Person)fakeRow;
// Cant cast on this way create error while compiling
DefaultRepository<Person> defRepo = new DefaultRepository<Person>();
Person fakePerson02 = defRepo.CreateInstanceOfModelFromDataRow(fakeRow);
在行编译“无法将类型'System.Data.DataRow'转换为'T'”时出现错误 modelInstance = (T)dataRow;
我也尝试过,(T)Convert.ChangeType(dataRow, typeof(T));但没有运气。
是否可以进行这种转换?