找到一个例子,我无法理解它的含义。
How can I read from a DataRow or a DataReader using the same code?
我不知道如何转换接口和两个类来回答C#?
编辑:这是我要转换为C#的代码:
Interface IIndexer
Default ReadOnly Property Item(ByVal index As String)
End Interface
Class DataReaderWrapper
Implements IIndexer
Private ReadOnly _reader As IDataReader
Public Sub New(reader As IDataReader)
_reader = reader
End Sub
Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
Get
Return _reader(index)
End Get
End Property
End Class
Class DataRowWrapper
Implements IIndexer
Private ReadOnly _row As DataRow
Public Sub New(row As DataRow)
_row = row
End Sub
Public ReadOnly Property Item(index As String) As Object Implements IIndexer.Item
Get
Return _row(index)
End Get
End Property
End Class
答案 0 :(得分:1)
据我所知,没有相应的东西。有关Default关键字的详细信息,请访问MSDN。
您在这里看到的是Indexer in an Interface。
转换后的代码如下所示:
interface IIndexer
{
object this[string index] { get; }
}
class DataReaderWrapper : IIndexer
{
private readonly IDataReader _reader;
public DataReaderWrapper(IDataReader reader)
{
_reader = reader;
}
public object this[string index]
{
get { return _reader[index]; }
}
}
class DataRowWrapper : IIndexer
{
private readonly DataRow _row;
public DataRowWrapper(DataRow row)
{
_row = row;
}
public object this[string index]
{
get { return _row[index]; }
}
}
答案 1 :(得分:0)
像这样的东西会起作用,不清楚“默认属性”是什么意思:
var loadedItems = new List<Item>();
SqlDataReader dr = GetItemsDataReader();
while(dr.Read()){
Item item = GetItemFromData(dr);
loadedItems.Add(item);
}
private Item GetItemFromData(SqlDataReader dr){
var loadedItem = new Item();
loadedItem.ID = dr["ID"];
loadedItem.Name = dr["Name"];
'etc., etc.'
Return loadedItem
}