【问题标题】:Casting from type to object back to type returns null从类型转换为对象返回类型返回 null
【发布时间】:2017-11-21 11:41:36
【问题描述】:

我有一个对象RS2005.ParameterValue[] parameters。我声明它的标签、名称和值。然后我将它传递给另一个函数,我们称之为 A。 当我尝试将其分配给函数 A 内 RS2005.ParameterValue[] 类型的临时变量 temp 时,即使对象不为空(根据调试器),它也会返回 null。此功能在几个版本前运行良好,但最近它停止工作,我想知道它是否与将 Visual Studio 版本更改为 2015 有关?

原函数

RS2005.ParameterValue[] parameters = new RS2005.ParameterValue[5];
parameters[0] = new RS2005.ParameterValue();
parameters[0].Label = "a";
parameters[0].Name = "a";
parameters[0].Value = "AA";
.....

frontPageBytes = Functions.functionA(parameters);

函数A

 public static byte[] functionA(object parameters)
    {
        byte[] bytes = null;
        RS2005.ReportExecutionService rsExec = new RS2005.ReportExecutionService();
        rsExec.Credentials = new NetworkCredential(RSServerUserLogin, RSServerUserPassword, RSServerUserDomain);
        rsExec.Url = RSServerWSURI;
        string deviceInfo = string.Format(@"<DeviceInfo><OutputFormat>{0}</OutputFormat></DeviceInfo>", "PDF");

        RS2005.ExecutionInfo ei = rsExec.LoadReport(rdlPath, null);
        RS2005.ParameterValue[] temp = parameters as RS2005.ParameterValue[];  <---------- This temp is null

        rsExec.SetExecutionParameters(parameters as RS2005.ParameterValue[], "en-us"); <---------- this throws an exception because parameters is null
        ....
}

【问题讨论】:

  • "即使对象不是空的"哪个对象? parametersfunctionA 内?到目前为止,我无法重现您的问题
  • 也许是个愚蠢的问题,但为什么不将functionA的类型改为public static byte[] functionA(RS2005.ParameterValue[] parameters),并获得编译时类型安全的帮助呢?通过object 再返回似乎是不必要的风险。
  • @StuartLC 或者使用直接演员而不是演员。

标签: c# reporting-services casting parameter-passing


【解决方案1】:

我找到了答案。我在问题中没有提到的是,功能 A 和原始功能在不同的项目中。这与我必须转换为 object 然后返回 RS20005.ParameterValue[] 的原因有关。

我没有注意到项目 A 和项目 B 的报表查看器版本略有不同。当我使用as 进行转换时,C# 试图通过返回 null 来拯救我,这使我无法找到错误。只有在直接铸造后,我才找到问题的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2013-10-30
    相关资源
    最近更新 更多