How to Bind typeparam with property in Blazor?
i have a generic Component which is a generic form and i get properties in a generic way then i pass them to the components depend on the property type like (int,string...etc).
Inside some entity i have relation and class. for example i have Blog entity in Post entity.
then i want to get the Blog and bind it to my anoteher generic components which name is SQEntityPicker. and this picker require some parameter and typeparam the only problem i have is typeparam TValue , I must write it like this "@typeparam TValue where TValue : class, new()" in my case. Then it would must to pass TValue whenever i use SQEntityPicker. As i explained i get properties in a generic way then i don’t know how to pass the property to the typeparam and bind it.
i share all of the codes for both components.
8 Replies
Can you use $paste please? It's more friendly for mobile users (me) :)
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Generally you may supply the generic type argument to generic components by using the type parameter name like a component parameter, but instead of supplying to it a value you supply a type.
sometimes the compiler is able to infer type arguments based on other parameters you supply
BlazeBin - cdalnflmmgey
A tool for sharing your source code with the world!
that is right the compiler will do that if i just define TValue like this (@typeparam TValue) but if i define it this way (@typeparam TValue where TValue : class, new()) then the compiler will not infer that and it would be must to define TValue from the parent component when the child used.
And in my case i have to define TValue this way (@typeparam TValue where TValue : class, new())
Well it looks to me like your
@bindProperty
is available in a weakly typed manner only; you're using reflection to obtain information about it etc. etc.
You can't go from weakly typed to I suddenly have the type information available at compile time without is
conversions, casts etc.
None of which is robust in my opinion.well. what should i do there? could give an example code?
You can try making your
SQEntityPicker
weakly typed, so as to accept an object
instead of TValue
. Your constraints class, new()
don't give you much more information than the object
type itself