Skip to content

RuntimeTypeModel.CanSerializeContractType has some counter-intuitive behavior. #701

@alennartz

Description

@alennartz

Hey,

I'm observing some counter intuitive behavior with regards to what types are reported as serializable. Not sure if this is by design or not so here goes.

What I'm trying to accomplish

Obtain an instance of a RuntimeTypeModel that will return false on bool CanSerializeContractType(Type t) unless that type has been explicitely annotated by a ProtoContractAttribute or is a List of such types. Now I can obviously just do the attribute check myself but I was expecting that I could configure an instance to basically behave in this way.

What I have tried:

var strictModel = RuntimeTypeModel.Create("strict");
strictModel.AutoAddProtoContractTypesOnly = true;
strictModel.InferTagFromNameDefault = false;

What I have observed

Some types even when not annotated by the attribute will come back as being supported by the type model. I have not been super exhaustive in my testing but what i have observed is that the types that seem to magically work are types that have get only properties where each of those properties can be found as a constructor parameter and that the constructor parameter has the same name (case insensitive) as the property.

The type definitions I have tested with

public class TypeA
{
	
}

public class TypeB
{
	public int Value { get; }
	
	public TypeB(int value)
	{
		
	}
}

public class TypeC
{
	public int Value { get; }
	
	public TypeC(int notValue)
	{
			
	}
}

public class TypeD
{
	public int Value { get; set; }

	public TypeD(int value)
	{

	}
}

public class TypeE
{
	public int Value { get; set; }

	public TypeE()
	{

	}
}

Out of all these types TypeB is the only one that returns true.

The Questions

  • Is this intended behavior?
  • Is there a way of getting an instance of RuntimeTypeModel that behaves the way I would like?
  • Why are the other types in my test not considered to be serializeable the same way TypeB is?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions