Skip to content
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

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

Open
butler1233 opened this issue Apr 19, 2021 · 2 comments

Comments

@butler1233
Copy link

butler1233 commented Apr 19, 2021

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!

@jspuij
Copy link
Owner

jspuij commented Apr 29, 2021

@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.

@butler1233
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants