Error: Type <nested-type> does not have a public constructor when running as a native executable
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 413
- Forks
- 72
- PR merge metrics
- No merged PRs in 30d
Description
I'm getting this error when deserializing an object with a list of nested objects, but only when running as a native win-x64 executable. It complains about my nested custom type not having a public constructor while it has one.
SharpYaml.YamlException: (Lin: 1, Col: 4, Chr: 26) - (Lin: 1, Col: 4, Chr: 26): Type 'SomeCustomType' does not have a public constructor. Use 'SharpYaml.Serialization.YamlConstructorAttribute' or 'System.Text.Json.Serialization.JsonConstructorAttribute' to opt into a non-public constructor.
---> System.NotSupportedException: Type 'SomeCustomType' does not have a public constructor. Use 'SharpYaml.Serialization.YamlConstructorAttribute' or 'System.Text.Json.Serialization.JsonConstructorAttribute' to opt into a non-public constructor.
at SharpYaml.Serialization.Converters.YamlObjectConverter`1.SelectDeserializationConstructor(Type) + 0x3e5
at SharpYaml.Serialization.Converters.YamlObjectConverter`1.Contract.Create(Type, YamlReaderWriterBase) + 0xace
at SharpYaml.Serialization.Converters.YamlObjectConverter`1.Read(YamlReader) + 0x103
--- End of inner exception stack trace ---
at SharpYaml.Serialization.Converters.YamlObjectConverter`1.Read(YamlReader) + 0x393
at SharpYaml.Serialization.YamlConverter`1.Read(YamlReader, Type) + 0xa
at SharpYaml.Serialization.Converters.SequenceReadHelpers.PopulateCollection[TElement](YamlReader, ICollection`1, YamlConverter&, String) + 0x138
at SharpYaml.Serialization.Converters.YamlIListConverter`1.Populate(YamlReader, Type, Object) + 0x66
at AppConfigContext.ReadObjectCore0(YamlReader, AppConfigContext.RuntimeCustomConverterCache) + 0x270
at AppConfigContext.ReadValue0(YamlReader, AppConfigContext.RuntimeCustomConverterCache) + 0xe0
at SharpYaml.YamlSerializer.DeserializeCore[T](YamlTypeInfo`1, String) + 0x46
Here is as sample yaml file:
root_object:
- name: text 1
value: other text 1
- name: text 2
value: other text 2
And a sample C# code:
using SharpYaml.Serialization;
using System.Text.Json.Serialization;
public class SomeCustomType
{
public SomeCustomType()
{
Name = string.Empty;
Value = string.Empty;
}
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("value")]
public string Value { get; set; }
}
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
public class RootClass
{
[JsonPropertyName("root_object")]
public IList<SomeCustomType> RootObject { get; } = new List<SomeCustomType>();
}
[YamlSerializable(typeof(RootClass))]
[YamlSerializable(typeof(SomeCustomType))]
public partial class RootClassContext : YamlSerializerContext
{}
The project has the following settings:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>SomeProject.Program</StartupObject>
<PublishAot>True</PublishAot>
</PropertyGroup>
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at SharpYaml.Serialization.Converters.YamlObjectConverter.SelectDeserializationConstructor(Type), using the provided RootClassContext, SomeCustomType, and PublishAot=true reproduction. Verify how constructor selection differs for the native win-x64 executable. Done means the sample YAML deserializes its nested list without the public-constructor exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100