我正在使用 Interactive Brokers C# API 并尝试从AccountSummary
对象创建数据表,但表中的行被覆盖并且只显示最后一个值。
我不知道发生了什么事。我认为可能是属性类型与列名匹配,因此它覆盖在同一行的顶部。
预期产出
public static DataTable ConverToDataTable(object obj)
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
obj.GetType().GetProperties().ToList().ForEach(f =>
{
try
{
f.GetValue(obj, null);
dt.Columns.Add(f.Name, f.PropertyType);
dt.Rows[0][f.Name] = f.GetValue(obj, null);
}
catch
{
}
});
return dt;
}
这是调用 ConvertToDataTable() 方法的方法。
public void UpdateUI(IBMessage message)
{
switch (message.Type)
{
case MessageType.AccountSummary:
ConverToDataTable((AccountSummaryMessage)message);
break;
case MessageType.AccountSummaryEnd:
HandleAccountSummaryEnd();
break;
case MessageType.AccountValue:
HandleAccountValue((AccountValueMessage)message);
break;
case MessageType.PortfolioValue:
HandlePortfolioValue((UpdatePortfolioMessage)message);
break;
case MessageType.AccountDownloadEnd:
break;
case MessageType.Position:
HandlePosition((PositionMessage)message);
break;
case MessageType.PositionEnd:
break;
}
}
这是我试图从中构建数据表的对象。
public class AccountSummaryMessage : IBMessage
{
private int requestId;
private string account;
private string tag;
private string value;
private string currency;
public int RequestId
{
get { return requestId; }
set { requestId = value; }
}
public string Account
{
get { return account; }
set { account = value; }
}
public string Tag
{
get { return tag; }
set { tag = value; }
}
public string Value
{
get { return this.value; }
set { this.value = value; }
}
public string Currency
{
get { return currency; }
set { currency = value; }
}
public AccountSummaryMessage(int requestId, string account, string tag, string value, string currency)
{
Type = MessageType.AccountSummary;
RequestId = requestId;
Account = account;
Tag = tag;
Value = value;
Currency = currency;
}
}