繁体   English   中英

System.InvalidCastException:Object 不能存储在这种类型的数组中。 在 C#

[英]System.InvalidCastException: Object cannot be stored in an array of this type. in C#

在使用 setter 为下面提到的 object 分配值时,出现以下错误。 这是我的代码。

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); :. 请协助!!

该变量是一个字符串数组 arrays。 您正在尝试将其中一个 arrays 的值设置为字符串数组 arrays,而不是单个字符串数组,这是无效的。 语法上讲,您可以这样做:

mmultiInfoValues2.AttributeValues = array;

但在不确切知道设置AttributeValues情况下,不清楚这是否会实现您想要的。 也许您需要将 arrays 从array复制到mmultiInfoValues2而不仅仅是更改引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM