【问题标题】:System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'System.IO.StreamWriter'System.InvalidCastException:无法将“System.Object”类型的对象转换为“System.IO.StreamWriter”类型
【发布时间】:2013-11-18 04:09:40
【问题描述】:

我尝试将 Object 变量转换为 StreamWriter。但是,它不起作用。什么错误?

StreamWriter file = (StreamWriter) myObject;

【问题讨论】:

  • 嗯,它是System.Object - 它不是System.IO.StreamWriter。你从哪里得到它,你为什么期望能够将它投射到StreamWriter
  • myObject 实际上是一个使用代码Object myObject = Dts.Variables["yourStreamWriterHere"].Value; 获得的SSIS 变量,这里不用担心。让我们看看 c#。
  • @JonSkeet - 我的答案如下。

标签: c# ssis .net-3.0


【解决方案1】:

又是一个愚蠢的时刻。我已经注释掉了执行此操作的行 - Object myObject = Dts.Variables["yourStreamWriterHere"].Value;所以,myObject 里面什么都没有。那里有空吗?在Java中它是。此外,java 为您提供了一个不错的 nullPointerException。但是,Visual Studio 给您一个神秘的“Micro$lop™:找不到错误原因。请联系 Billy 以获得更多帮助。请随身携带信用卡以防万一。呼叫中心等待时间各不相同”。只是烦人。

无论如何,你得到的真正错误是 -

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Unable to cast object of type 'System.Object' to type 'System.IO.StreamWriter'.
   at ST_ffrrefr939322939392dfr.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

【讨论】:

  • 是的,如果你没有分配它,这将是一个空值。你没有得到 NullReferenceException 因为没有解除引用。
  • @blasto : 是的,那么它包含 null,并且当您在其上调用任何函数时会抛出 null refrence 异常
  • @blast - 在 C# 中,null 的对象实际上确实有一个类型;您可以将null 转换为您想要的任何类型。例如,您可以将 null 作为方法的参数传递,只要您传递的 null 与方法的所述参数的类型相同。
  • @ZacharyKniebel - 为什么允许这样做?
  • @blasto 取消引用是当您尝试使用引用的值做某事时发生的。在您的示例中, myObject 是对对象的引用。例如,如果您要调用 myObject.ToString(),您将取消引用该引用,因为它引用的是 null,您将得到 NullReferenceException。
【解决方案2】:

试试这个:

if (myObject is StreamWriter) 
{
    var file = (StreamWriter) myObject as StreamWriter;
}

【讨论】:

  • 无论myObject 是否为null 都有效。但是,在 C# 中,null 的对象与未初始化的对象之间存在差异。因此,如果 myObject 已通过 object myObject = null 声明和初始化,但如果它刚刚通过 object myObject; 声明而没有初始化,则上述方法有效。在您的情况下,我假设 myObject 是方法的参数(object 类型)并且传递了 StreamWriter。只要 StreamWriter 已经被初始化,即使是 StreamWriter sw = null;,上面的操作也不会出错。
【解决方案3】:

在转换之前,检查是否可以使用is关键字将其转换为StreamWriter对象,如下所示:

if(myObject is StreamWriter)
{
//can be cast
}
else
{
//can not be cast
}

【讨论】:

    【解决方案4】:

    myObject 的值不是(可转换为)StreamWriter。

    【讨论】:

    • 我现在仔细注意到了value这个词。说得通。我的值为 null。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多