0
對於某些調試我需要得到由類TVPDataCollection的方法的IEnumerator IEnumerable.GetEnumerator()產生的SqlDataRecord的列表如何調用通用方法IEnumerator <> IEnumerable <>。GetEnumerator()?
驗證碼:
TVPDataCollection<AttributiDocumento> oAttributiDocumentoList = new TVPDataCollection<AttributiDocumento>();
AttributiDocumento doc1 = new AttributiDocumento();
AttributiDocumento doc2 = new AttributiDocumento();
oAttributiDocumentoList.Add(doc1);
oAttributiDocumentoList.Add(doc2);
var x = oAttributiDocumentoList.GetEnumerator();
x是AttributiDocumento的名單,但我需要一個SqlDataRecord的列表,因爲我想檢查這些值。調用返回SqlDataRecord的GetEnumerator()的方法是什麼?
public class TVPDataCollection<T> : List<T>, IEnumerable<SqlDataRecord> where T : class
{
IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
{
List<SqlMetaData> records = new List<SqlMetaData>();
var properties = typeof(T).GetProperties();
foreach (var prop in properties)
{
SqlType oSqlType = GetSqlType(prop);
if (oSqlType.UseSize)
records.Add(new SqlMetaData(prop.Name, oSqlType.SqlDbType, oSqlType.Size));
else
records.Add(new SqlMetaData(prop.Name, oSqlType.SqlDbType));
}
SqlDataRecord oSqlDataRecord = new SqlDataRecord(records.ToArray());
foreach (T data in this)
{
for (int i = 0; i < properties.Length; i++)
{
oSqlDataRecord.SetValue(i, properties[i].GetValue(data, null));
}
yield return oSqlDataRecord;
}
}
}
public class AttributiDocumento
{
public int IdProvPart { get; set; }
//uso stringa in quanto con data agigunge 2 ore del fuso orario
public DateTime Data { get; set; }
public int Articolo { get; set; }
public int ExArticolo { get; set; }
public int DocVer { get; set; }
[LenAttribute(255)]
public string Altro { get; set; }
public AttributiDocumento()
{
this.Data = new DateTime(1900, 1, 1);
this.Altro = string.Empty;
}
}
您可以強制轉換,然後直接在'for each'中使用結果,而不用調用'GetEnumerator';這避免明確地與枚舉器一起工作,並仍然得到正確的重載。 – MicroVirus
關於你的編輯:'IEnumerator'不是一次性的,所以最後一句沒有意義。 – MicroVirus
@微型病毒我認爲它確實:https://msdn.microsoft.com/en-us/library/78dfe2yb%28v=vs.110%29.aspx –