0

找到一个例子,我不明白它的意思。

如何使用相同的代码从 DataRow 或 DataReader 读取数据?

我不知道如何转换接口和两个类以响应 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
4

2 回答 2

1

据我所知,没有等价物。有关Default关键字的更多信息,请参见 MSDN。

您在这里看到的是Interface 中的 Indexer

转换后的代码将类似于以下内容:

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]; }
    }
}
于 2013-03-10T14:37:36.790 回答
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
}
于 2013-03-10T14:26:00.220 回答