On 04.04.2013 20:57, Neale Ferguson wrote:
Hi,
I¹m looking at fixing an incompatibility between .NET and Mono when it
comes to Serialization. Bugzilla 11294 describes the situation but in brief
it is a case that when .NET serializes something that has a
TypeForwardedFrom attribute that information gets put into the serialized
object, but mono does not. It appears that all mono does when it encounters
that attribute it simply stores it away and nothing uses it. To emulate the
.NET behavior that information needs to be accessible in metadata about the
type and that information uses by the Serializers.
I¹m trying to work out how the information captured by
TypeForwardedFromAttribute.cs can be made available at runtime to the
serializer. My knowledge of how mcs operates and what the contents of
metadata are and how to access them is my roadblock. So any reading/code
that I can be directed to would be a great help.
You don't need to know how mcs operates. The TypeForwardedFrom
information can be obtained via reflection:
public static class TypeExtensions
{
// Returns the assembly name of a type while considering
// TypeForwardedFromAttribute.
public static string GetAssemblyName (this Type self)
{
var attrs = self.GetCustomAttributes(typeof
(TypeForwardedFromAttribute), false);
if (attrs.Length == 0)
return self.Assembly.FullName;
else
return ((TypeForwardedFromAttribute)attrs
[0]).AssemblyFullName;
}
}
I've attached a full sample which proves that MS.NET is simply
emitting TypeForwardedFromAttribute.AssemblyFullName for the
assembly name during serialization.
Robert
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
public static class TypeExtensions
{
public static string GetAssemblyName (this Type self)
{
var attrs = self.GetCustomAttributes(typeof (TypeForwardedFromAttribute), false);
if (attrs.Length == 0)
return self.Assembly.FullName;
else
return ((TypeForwardedFromAttribute)attrs [0]).AssemblyFullName;
}
}
[TypeForwardedFrom(SomeOtherAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)]
[Serializable]
public class TestClass
{
}
public class Program
{
static void Main ()
{
Console.WriteLine (TypeForwardedFrom:);
Console.WriteLine (typeof (TestClass).GetAssemblyName ());
Console.WriteLine (Serialization dump:);
Dump (new TestClass ());
}
static void Dump (object obj)
{
using (var stm = new MemoryStream ()) {
var fmt = new BinaryFormatter ();
fmt.Serialize (stm, obj);
Dump (stm.ToArray (), 72);
}
}
static void Dump(byte[] bytes, int width)
{
for (int i = 0; i bytes.Length; i++) {
byte b = bytes[i];
if (b 32)
b = (byte) '.';
Console.Write (Convert.ToChar(b));
if ((i + 1) % width == 0)
Console.WriteLine ();
}
if (bytes.Length % width != 0)
Console.WriteLine ();
}
}
___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list