-
Notifications
You must be signed in to change notification settings - Fork 47
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
question: how to instantiate a different type during serializing (or, is IDictionary<k,v> bugged?) #463
Comments
Issue-Label Bot is automatically applying the label Links: app homepage, dashboard and code for this bot. |
👍 issue-label-bot. you done good. |
Hey @binary1230 thank you for writing in with the kind words and for all the great detail! From the outset I would say we are indeed running into a limitation of how ExtendedXmlSerializer views dictionaries. I will look into this, however, and see if there is an easy fix for this (or perhaps a workaround with an extension). As for your question on querying generic types, you have for sure run into a limitation. 😁 Right now our querying constructs only allow for direct type definitions. Adding such functionality would be a challenge, but if someone would like to embark on the path of providing a PR that allows for such capability I would not stop them. :) 👍 |
Branch issues/other/i463 created! |
Cool! thanks for looking at it, I really appreciate it. The project I'm using this for is over here, I actually did end up figuring out how to build a self-registering system for generic types though.... it's probably too hacky for real use. However, it's a neat proof of concept: /~https://github.com/binary1230/DiztinGUIsh/blob/master/Diz.Core/util/ObservableDictionaryAdaptor.cs#L18 The key being I use reflection to go through the loaded assemblies, find all the combo of generic types that I want to serialize, and then create a list of functions to apply with ConfigurationContainer. So, effectively, at the start of serialization, I call this on each generic type automatically: (here, OdWrapper<TKey,Tvalue> is my custom type that I want to apply .Member().Ignore to every instance of it) public static IConfigurationContainer AppendDisablingType<TKey, TValue>(this IConfigurationContainer @this)
=> @this
.EnableImplicitTyping(typeof(OdWrapper<TKey, TValue>))
.Type<OdWrapper<TKey, TValue>>()
.Member(x => x.Dict).Ignore(); |
Wow! Very cool @binary1230. It's a little humbling to see how others are using this library and how they are integrating with it. Hopefully someone will be able to utilize your work in their own way as well. I think it would be cool to create a library at some point, maybe Speaking of which. 😁 I have a build for you to try out here: Please let me know how that treats you and if everything looks good I will put it into the build slated for Tuesday. 🤞 |
YESSSS. that does the trick really nicely. thank you so much! I've integrated it into IsoFrieze/DiztinGUIsh#18 and was able to rip out the wrapper stuff. it all seems to be working great! Thanks again! |
Cool! Very glad to hear that, @binary1230. I will get this merged and then pushed out to NuGet this Tuesday (our usual deployment time). I will update here when it is ready. |
This is now available in NuGet: https://www.nuget.org/packages/ExtendedXmlSerializer/ Please do let me know of any issues you encounter and I will take a look into it for you. 👍 Closing for now.
|
First: HI! This library is amazing. Thank you for working on it.
I -think- I have a likely a documentation/understanding question, but, there's a small chance this might be a bug.
If I use a regular
Dictionary<somekey,somevalue>
class, serialization works great.ISSUE:
When I tried to use a third-party dictionary class, I was seeing exceptions with an invalid attempt to cast to
IDictionary
Here is a test reproducing the issue with a type called DictionaryExtended that inherits from IDictionary<TKey, TValue>
I expect that to work, but, I see the following exception when I run it:
ExtendedXmlSerializer is rightfully complaining, this type can't be cast to IDictionary. For instance, if I run the same cast manually, it fails:
That cast is happening inside ExtendedXmlSerializer at DictionaryEnumerators.cs:17
the key part there that fails is when it tries this cast:
full stack trace below (this is from a slightly different project but same result)
If I change DictionaryExtended to ALSO inherit from IDictionary, I get a different error about converting keys and values (would need to track it down further to reproduce).
I know I can't directly cast from IDictionary to IDictionary<TKey, TValue>. I'm looking for advice on best way to work around this in ExtendedXmlSerializer. It feels like not being able to serialize an IDictionary<k,v> derived type is a bug, but, I'm not sure. Or, maybe I messed up something with my enumerators, or maybe this is all a crazy approach. I just want serialized dictionaries :)
I suspect the new Interceptor approach in #451 is a good way to solve this, but, I couldn't get it working. That seems like the right way to say 'hey, anytime you think you should cast to IDictionary for an object of type DictionaryExtended, stop, and instead call my code so I can just create a DictionaryExtended for you, then pick it up from there'.
Either that or, could this be a weird interaction with the serializer not handling the generic params correctly? i.e. it's trying to create and cast to IDictionary when it should instead be casting to IDictionary<int,string>
And perhaps it's an issue with ExtendedXmlSerializer thinking it's OK to cast DictionaryExtended to IDictionary when in fact it's not actually derived form that interface (just a confusingly similarly named IDictionary<TKey,TValue>)?
Unrelated question [sorry], is there a way to use .TypeOf<>() to select all generic classes?
i.e. say I have that same DictionaryExtended<K,V> class and it has a member named Test i'd like to ignore.
Right now it seems I have to type:
.TypeOf<DictionaryExtended<int,string>>().Member(x => x.Test).Ignore()
i.e. I have to specify every combo of generic params (say,
<int,string>,<int,int>,<SomeType,SomeOtherType>
etc).Did I miss a way to just say something like:
.TypeNameMatches("DictionaryExtended").Member(x => x.Test).Ignore()
Thanks so much for your time, I know it's tight right now from your other issue. \m/
The text was updated successfully, but these errors were encountered: