Exception serialization error - .NET framework bug ??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Having classes as below:

-------------------------------------------------------
class X<T> where T : Y, new()
{
public void Test()
{
throw new Exception("XXXX");
}
}

class Y
{
int a;
}

class Z : Y
{
int b;
}
-------------------------------------------

and executing code:

-------------------------------------------
X<Z> variable = new X<Z>();
try
{
variable.Test();
}
catch (Exception ex)
{
Stream writer = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(writer, ex);
writer.Close();
}
---------------------------------------

I've got an error trying to serialize exception (generic parameter don't
meet contraints).
It happens alway when trying to serialize exception thrown from generic
class when generic parameter is constrained with inheritance.
If you remove where contraint exception is serialized succesfully.

I use VS 2005 beta 2.
Is it .NET framework bug or I'm missing something important?
 
Exception looks like this:

-----------------------------------------------------------------------------------
System.ArgumentException was unhandled
Message="GenericArguments[0], 'System.Object', on
'WindowsApplication4.X`1[T]' violates the constraint of type 'T'."
Source="mscorlib"
StackTrace:
at System.RuntimeType.ValidateGenericArguments(MemberInfo definition,
Type typeContext, MethodInfo methodContext, Type[] genericArguments, Type[]
genericParamters)
at System.RuntimeType.MakeGenericType(Type[] instantiation, Boolean
verify)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
at System.RuntimeType.GetMethodBase(RuntimeTypeHandle
reflectedTypeHandle, RuntimeMethodHandle methodHandle)
at System.Exception.GetObjectData(SerializationInfo info,
StreamingContext context)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object
obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,
ObjectWriter objectWriter)
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object
obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,
ObjectWriter objectWriter)
at
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object
graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
-----------------------------------------------------------------------------------
 
Back
Top