Skip to content
\n

When I try to run it from Test Explorer with MTP enabled, I see the following warning in the Test Output window:

\n

Error creating theory test case for for 'WhenCreateDataCollection.AndCounterIsSupported_ReturnsCollectionWithCounterType'; falling back to single test case. Exception message: 'Cannot serialize type 'System.Diagnostics.PerformanceCounterType' because it lives in the GAC Parameter name: value'

\n

I thought I could fix the warning by creating a custom IXunitSerializer. When I step through the test in the debugger it hits my breakpoint in the call to IsSerializable (which returns true), but it never hits my breakpoint in the Serialize method and I still get the warning. This is what my serializer looks like:

\n
public class PerformanceCounterTypeSerializer : IXunitSerializer\n{\n    public Boolean IsSerializable(Type type, Object value, out String failureReason)\n    {\n        if (type == typeof(PerformanceCounterType))\n        {\n            failureReason = null;\n            return true;\n        }\n\n        failureReason = $\"Theory data of type {type.FullName} is not currently supported.\";\n        return false;\n    }\n\n    public String Serialize(Object value)\n    {\n        if (value is PerformanceCounterType performanceCounterType)\n        {\n            return performanceCounterType.ToString();\n        }\n\n        throw new NotSupportedException($\"Custom serialization (of type {value.GetType().FullName}) is not currently supported.\");\n    }\n\n    public Object Deserialize(Type type, String value)\n    {\n        if (type == typeof(PerformanceCounterType))\n        {\n            return (PerformanceCounterType)Enum.Parse(typeof(PerformanceCounterType), value);\n        }\n\n        throw new NotSupportedException($\"Custom deserialization (of type {type.FullName}) is not currently supported\");\n    }\n}\n
\n

I assume the custom serializer was registered properly because otherwise my IsSerializable method wouldn't be called. Am I doing something else wrong? Is there a better way to handle this kind of test data?

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

I am able to reproduce this with

\n
[assembly: RegisterXunitSerializer(typeof(PerformanceCounterTypeSerializer), typeof(PerformanceCounterType))]
\n

This is caused by inappropriate GAC-checking code in TypeHelper.GetTypeName, logic which should've been solely relegated to EnumSerializer.

\n

This is fixed by 4f4077c and available in v3 2.0.1-pre.6 https://xunit.net/docs/using-ci-builds

","upvoteCount":1,"url":"https://github.com/xunit/xunit/discussions/3217#discussioncomment-12469457"}}}

Using a custom serializer for a type in the GAC #3217

Discussion options

You must be logged in to vote

I am able to reproduce this with

[assembly: RegisterXunitSerializer(typeof(PerformanceCounterTypeSerializer), typeof(PerformanceCounterType))]

This is caused by inappropriate GAC-checking code in TypeHelper.GetTypeName, logic which should've been solely relegated to EnumSerializer.

This is fixed by 4f4077c and available in v3 2.0.1-pre.6 https://xunit.net/docs/using-ci-builds

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@bradwilson
Comment options

@bradwilson
Comment options

@sheila-stewart
Comment options

@bradwilson
Comment options

@sheila-stewart
Comment options

Answer selected by sheila-stewart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants