c# - System.InvalidCastException:对象不能存储在这种类型的数组中。在 C# 中
问题描述
使用 setter 为下面提到的对象分配值时出现以下错误。这是我的代码。
public override void Execute(OrderedDictionary htFileList)
{
mlogger.Error("RemoveRestrictions start..");
string htFile = (string) htFileList[(object) "connection_id"];
try
{
string str = "select distinct I.[Security Id] AS [sec_id] FROM IVPSecMasterVendor.dbo.vw_MSCIRestritionList_MSCIRestrictionFeed_EquityCommonStock F RIGHT OUTER JOIN IVPSecMaster.dbo.vwCommonAttributes I ON I.isin = F.ISIN WHERE I.[Security Id] like 'EQST%' and F.ISIN IS NULL; select TD.display_name from IVPSecMaster.dbo.ivp_secm_attribute_details AD JOIN IVPSecMaster.dbo.ivp_secm_template_details TD ON TD.attribute_id = AD.attribute_id where sectype_table_id = 20 and to_show = 1";
SqlConnection sqlConnection = new SqlConnection();
DataSet dataSet = new DataSet();
SqlCommand selectCommand = new SqlCommand();
sqlConnection.ConnectionString = htFile;
selectCommand.Connection = sqlConnection;
selectCommand.CommandText = str;
new SqlDataAdapter(selectCommand).Fill(dataSet);
mlogger.Error("Query Output[0] is"+dataSet.Tables[0].Rows.Count);
SecMMultiInfoValues mmultiInfoValues1 = new SecMMultiInfoValues();
mlogger.Error("mmultiInfoValues1 created");
if (dataSet == null || dataSet.Tables.Count <= 1 || dataSet.Tables[0].Rows.Count <= 0 || dataSet.Tables[1].Rows.Count <= 0)
return;
string[][] array = dataSet.Tables[1].AsEnumerable().Select<DataRow, string[]>((Func<DataRow, string[]>) (x => new string[2]{ x.Field<string>("display_name"), "" })).ToArray<string[]>();
mlogger.Error("Dataset stored into array : string[][] array");
foreach(string[] arr in array)
{
foreach(string stri in arr)
{
mlogger.Error(" stri "+stri);
}
}
SecMMultiInfoValues mmultiInfoValues2 = new SecMMultiInfoValues();
mmultiInfoValues2.AttributeValues = new string[2][];
mmultiInfoValues2.AttributeValues.SetValue((String[][])array, 0);
mlogger.Error("mmultiInfoValues2 created");
mlogger.Error("Set values for mmultiInfoValues2");
SecMMultiInfoValues mmultiInfoValues3 = mmultiInfoValues2;
mlogger.Error("mmultiInfoValues3 created");
ThirdPartyUpdateService partyUpdateService = new ThirdPartyUpdateService();
mlogger.Error("partyUpdateService created");
foreach (DataRow row in (InternalDataCollectionBase) dataSet.Tables[0].Rows)
{
mlogger.Error("fetching secm record");
SecMRecord secMrecord = partyUpdateService.UpdateMultiInfoOn("SYSTEM", "Equity Common Stock - MSCI Restrictions", "Security ID", row["sec_id"].ToString(),"", "", "", "", new SecMMultiInfoValues[1]{ mmultiInfoValues3 });
mlogger.Error("secMrecord.Name : "+secMrecord.Name);
if (!secMrecord.IsSuccess)
{
EventLog.WriteEntry("Application", secMrecord.Name);
mlogger.Error("Failed" + secMrecord.Name);
}
}
}
catch (FileNotFoundException ex)
{
EventLog.WriteEntry("Application", ex.ToString());
mlogger.Error("Exception" + ex.ToString());
throw ex;
}
catch (ArgumentNullException ex)
{
throw ex;
}
catch (ArgumentOutOfRangeException ex)
{
throw ex;
}
catch (IOException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
mlogger.Error("RemoveRestrictions End..");
}
我在行 mmultiInfoValues2.AttributeValues.SetValue((String[][])array, 0); :. 请协助!!
解决方案
该变量是一个字符串数组的数组。您正在尝试将其中一个数组的值设置为字符串数组的数组,而不是单个字符串数组,这是无效的。从语法上讲,您可以这样做:
mmultiInfoValues2.AttributeValues = array;
但在不确切知道什么设置AttributeValues
的情况下,不清楚这是否会实现你想要的。也许您需要将数组从其中复制array
到,mmultiInfoValues2
而不仅仅是更改引用。
推荐阅读
- mongodb - 甚至无法连接到 Mongodb Atlas 使用UnifiedTopology: true,
- java - 使用 FirebaseDatabase 中的数据填充 Fragment 中的 RecyclerView
- javascript - 将新的键:值对“推送”到 JavaScript 对象上
- processing - 在处理 3 中的设备之间切换会改变事物的外观
- angular - Angular如何将参数传递给注入的服务
- c# - 如何在 C# 对象中放置一个列表?
- node.js - 无法从 Express.js 中的端点获取
- sql - 根据范围应用和求和值
- pandas - Pandas - 如何对 groupby() 的计数求和以按该总和排序
- python - 上传图片 python tkinter