-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
Allow extending genericInterfaceImplementations of DefaultObjectFactory #982
Comments
How are you wanting to extend that class? If you're wanting to add additional types you can do that by passing in a dictionary of type mappings. That linked method is just making sure the types are assignable and making a copy of the passed in dictionary. We could remove the |
Yes, that will be sufficient. Moreover, I realized that in my case extending But for mapping-like collections, I also need to tweak the serialization (mentioned here #236 (comment)). Currently, I do the following: static readonly IObjectFactory objectFactoryForSerialization = new CustomObjectFactoryForSerialization();
...
.WithObjectGraphTraversalStrategyFactory((typeInspector, typeResolver, typeConverters, maximumRecursion) =>
new FullObjectGraphTraversalStrategy(typeInspector, typeResolver, maximumRecursion, CamelCaseNamingConvention.Instance, objectFactoryForSerialization)) Where public class CustomObjectFactoryForSerialization : ObjectFactoryBase
{
public override bool GetDictionary(IObjectDescriptor descriptor, out IDictionary? dictionary, out Type[]? genericArguments)
{
genericArguments = descriptor.Type.GetInterfaceGenericArguments(typeof(IReadOnlyDictionary<,>));
if (genericArguments != null)
{
dictionary = (IDictionary)Activator.CreateInstance(typeof(ReadOnlyDictionaryAdapter<,>).MakeGenericType(genericArguments), descriptor.Value)!;
return true;
}
return base.GetDictionary(descriptor, out dictionary, out genericArguments);
}
public override object Create(Type type) => throw new NotSupportedException();
} Since I cannot currently derive from Removing |
Sounds good, it'll be done. |
Is your feature request related to a problem? Please describe.
It seems to me that there is currently no way to extend
defaultGenericInterfaceImplementations
ofDefaultObjectFactory
.YamlDotNet/YamlDotNet/Serialization/ObjectFactories/DefaultObjectFactory.cs
Lines 45 to 51 in 485daaa
Mappings are only added to
defaultNonGenericInterfaceImplementations
.YamlDotNet/YamlDotNet/Serialization/ObjectFactories/DefaultObjectFactory.cs
Lines 75 to 83 in 485daaa
Describe the solution you'd like
Provide a "simple" way to extend
defaultGenericInterfaceImplementations
.Describe alternatives you've considered
Modify copy of
DefaultObjectFactory
.Additional context
N/A
The text was updated successfully, but these errors were encountered: