ArgumentException: "Argument passed in is not serializable"

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

I am trying to pass a variable that has been disposed into a function
that accepts an object, and I get this exception.

What does it mean?
Why can't I pass in *anything* into a parameter of type object?
Doesn't it cover them all?

Zytan
 
It sounds like the problem isn't passing an object - that should work
fine. The problem is that the method in question expects certain
things from that object. *what* it wants is determined by the exact
method. What method is this? As an example... string.Concat expects to
be be able to call .ToString() on the arguments... if I pass it an
object that is (for some reason) going to throw an exception
on .ToString(), then string.Concat isn't going to be happy.

Marc
 
Zytan said:
I am trying to pass a variable that has been disposed into a function
that accepts an object, and I get this exception.

What does it mean?
Why can't I pass in *anything* into a parameter of type object?
Doesn't it cover them all?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

I suspect it's the method you're passing the parameter to that's
throwing the exception, but we won't know without more context.
 
Zytan said:
I am trying to pass a variable that has been disposed into a function
that accepts an object, and I get this exception.

What does it mean?
Why can't I pass in *anything* into a parameter of type object?
Doesn't it cover them all?

Take the following example:

class Foo
{
public Foo(){ }
public override ToString(){
throw new Exception();
}
}
class Bar
{
public Bar(){ }
public void PrintObject(object that){
System.Console.Println(that.ToString());
}
static void Main(string[] args){
Foo foo = new Foo();
Bar bar = new Bar();
bar.PrintObject(foo);
}
}

This will compile, but when it runs, bar.PrintObject(foo) will throw an
exception at runtime, since it calls Foo.ToString()

In your own example, you're passing an object to a method, but that
method expects that object to be serializable.
Serialization is really easy in C#. You can probably just change your
class definition from

class Foo
{
....
}

to

[Serializable]
class Foo{
....
}

and make sure that all the fields that must be stored in the serialized
data are public.

Alun Harford
 
As a final note; "disposed"; if it has been disposed, you should
probably be hands-off, i.e. once an object is disposed it is under no
obligation to honour *any* of the things it used to do. What are you
trying to get this disposed object to do, and why?

Marc
 
I'm sorry, I can't remember the exact consequences of the problem.
And trying to replicate it in a small app isn't working. I made a
Timer, and disposed it, and passed it into a function that accepts
object, and it takes it in fine. Even calls ToString() on it, to
print it out.

It sounds to me, from your responses, that the object did not accept
what the function was trying to do with it (say, as you've stated,
some class that throws inside of ToString()). But, I just made a
class that throws in its ToString(), and it just says that the
exception isn't handled.

I've tried make a class with IDisposable, and implementing Dispose,
and of course, this object can be passed as an object parameter
regarding of if it is constructed or disposed.

I just can't remember the consequences that caused this. The lack of
information in google indicates this is uncommon. I think it was
during my attempt to access my logging class's file stream after it
had been disposed of (during program shutdown).

My main question was to understand what "serializable" meant.

Thanks for your help.

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top