Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializer for Custom Array not generated... #31

Closed
jogibear9988 opened this issue Nov 13, 2015 · 4 comments
Closed

Serializer for Custom Array not generated... #31

jogibear9988 opened this issue Nov 13, 2015 · 4 comments

Comments

@jogibear9988
Copy link

I've a class with a Array:

class LcInformationResponse {
public LcInformations[] Infos {get;set;}
}

for this class, no correct serializer/deserializer is build!

I could do my own serializer for it, but then i have to use GetTypeIdAndSerializer, and also the 2 delegates have to be public!

@jogibear9988
Copy link
Author

Ok, it was my fault that the serializer was not created.

It works now, but it would be nice if I can access those properties so a class like this is possible:

class ArraySerializer : IStaticTypeSerializer
{
public bool Handles(Type type)
{
if (type.IsArray)
{
if (type.GetElementType().Namespace.StartsWith("BlaBla"))
{
return true;
}
}
return false;
}

    public IEnumerable<Type> GetSubtypes(Type type)
    {
        yield break;
    }

    public MethodInfo GetStaticWriter(Type type)
    {
        return typeof(ArraySerializer).GetMethod("Serialize", BindingFlags.Static | BindingFlags.Public);
    }

    public MethodInfo GetStaticReader(Type type)
    {
        return typeof(ArraySerializer).GetMethod("Deserialize", BindingFlags.Static | BindingFlags.Public);
    }

    public static void Serialize(Serializer serializer, Stream stream, object ob)
    {
        if (ob == null)
        {
            Primitives.WritePrimitive(stream, (int) 0);
            return;
        }

        SerializeDelegate<object> del;

        var array = (Array) ob;
        Primitives.WritePrimitive(stream, array.Length + 1);
        uint id = serializer.GetTypeIdAndSerializer(ob.GetType(), out del);
        Primitives.WritePrimitive(stream, id);


        for (int n = 0; n < array.Length; n++)
        {
            var obj = array.GetValue(n);
            if (obj != null)
            {
                id = serializer.GetTypeIdAndSerializer(obj.GetType(), out del);
                Primitives.WritePrimitive(stream, id);
                del(serializer, stream, obj);
            }
            else
            {
                Primitives.WritePrimitive(stream, (uint) 0);
            }
        } 
    }

    public static void Deserialize(Serializer serializer, Stream stream, out object ob)
    {
        int l1;
        uint l2;
        uint l3;

        Primitives.ReadPrimitive(stream, out l1);

        if (l1 == 0)
        {
            ob = null;
            return;
        }

        Primitives.ReadPrimitive(stream, out l2);
        var type = serializer.GetDeserializeTypeFromId(l2);
        var array = Array.CreateInstance(type.GetElementType(), l1 - 1);
        ob = array;
        object obj;
        for (int i = 0; i < l1 - 1; i++)
        {
            Primitives.ReadPrimitive(stream, out l3);
            if (l3 != 0)
            {
                var del = serializer.GetDeserializeTrampolineFromId(l3);
                del(serializer, stream, out obj);
                array.SetValue(obj, i);
            }
        }    
    }
}

@tomba
Copy link
Owner

tomba commented Nov 14, 2015

Sorry, I don't follow. What's the purpose of this class? What properties you need to access?

@jogibear9988
Copy link
Author

i will create a pull req

@jogibear9988
Copy link
Author

You see it here: #36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants