jspuij/Cortex.Net

Actions with generic-derived argument throw TypeLoadException when the type is initialised

Opened this issue · 2 comments

I just want to start off by saying this library is absolutely fantastic - it makes Blazor almost as magic as WPF with regards to binding stuff to the UI and it magically updating.

However, I ran into a somewhat niche issue with it when I was creating a razor component which included the following:

[Observable]
public ICollection<BetterKeyValuePair<string,string>> Options { get; set; }

(for reference, here's that type in all it's not exactly interesting glory)

public class BetterKeyValuePair<T1, T2>
    {
        public T1 Key { get; set; }
        public T2 Value { get; set; }

        public BetterKeyValuePair(T1 key, T2 value)
        {
            Key = key;
            Value = value;
        }

        public BetterKeyValuePair()
        {
            Key = default;
            Value = default;
        }
    }

I then had an action which was written like this;

[Action]
private void RemoveOption(BetterKeyValuePair<string,string> option)
{
    Options.Remove(option);
}

When I removed the [Action] attribute it worked, although then obviously it threw the whole "side effects are not allowed" which makes sense. But then I dropped the genericness from the BetterKeyValuePair (it probably won't ever be used aside from string,string anyway) and then it worked like a charm!

I'd have a stab at fixing it myself, and I might take a look later, but I thought I'd raise it first. I've asked somebody else who introduced me to this box of magic to verify that too.

Thanks!

@butler1233 For observable collections there is a whole lot of things going on to make sure that they trigger reactions when items are added, modified, removed or attributes change on the objects themselves. You're best served by using the built-in observable collection types for that. There is an observabledictionary that should be useable for you I guess. Let me know if that works.

Oh yeah the observable dictionaries work fine, it's just that the weird use case that this was in required something that looked like a dictionary, but wasn't. The TLDR is the user is editing what is essentially a dictionary, editing the keys and values on the page. However Keys don't like being edited, hence this... weirdness.