-
Notifications
You must be signed in to change notification settings - Fork 10k
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
TypeConverter for parameters #8493
Comments
We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
Fixes: #8493 Fixes: #9632 Fixes: #9339 Fixes: #8385 Fixes: 10077 This fix adds support for type converters as well as a few other minor things we were missing from binding. The key thing about supporting conversions is that we new can support arbitrary types with `@bind`. This means you can use it with generics, which is something many users have tried. Along with type converters we get Guid and TimeSpan from the BCL. The BCL also includes converters for types we're less interested in like `short`.
Fixes: #8493 Fixes: #9632 Fixes: #9339 Fixes: #8385 Fixes: 10077 This fix adds support for type converters as well as a few other minor things we were missing from binding. The key thing about supporting conversions is that we new can support arbitrary types with `@bind`. This means you can use it with generics, which is something many users have tried. Along with type converters we get Guid and TimeSpan from the BCL. The BCL also includes converters for types we're less interested in like `short`.
* Add support for TypeConverter Fixes: #8493 Fixes: #9632 Fixes: #9339 Fixes: #8385 Fixes: 10077 This fix adds support for type converters as well as a few other minor things we were missing from binding. The key thing about supporting conversions is that we new can support arbitrary types with `@bind`. This means you can use it with generics, which is something many users have tried. Along with type converters we get Guid and TimeSpan from the BCL. The BCL also includes converters for types we're less interested in like `short`. * Use correct NumberStyles * Fix culture * Core check
This has been added in preview 7 - #10730 |
How will this finally work? adding the type converter attribute in each property, or can the attribute be specified in the class so it's globally taken? |
Implement |
I'm clear about registering the TypeConverter on the class, this way the converter can parse the string, but why is the ToString necessary? |
ToString is used to convert the value to a string. We don't use the type converter at this point because we don't easily know what cases require it. |
The idea of using the TypeConverter is to parse the string to the actual class, not the other way round. Can you clarify? I'm really confused by your statement. Will TypeConverter work or not? |
Yes, the type converter is used in parsing. |
Also bool and int are special, why only those? Why not enums? |
No, I have to disagree. Those are using the normal C# syntax for bools and ints. Likewise, you use the normal C# syntax for enums and other types. |
In preview7 that's incorrect, bool's can be specified using the c# syntax by appending the "@" before "true" or "false" terms, but they are also "special" and can be set without using the "@". |
Whether or not an <MyComponent
BoolParameter="<this is C#>"
StringParameter="<this is text>"
AnotherStringParameter="@<now this is C#>"/> |
The same reasoning would apply for any other type that has an implicit string cast operator. The syntax end up being gross. |
My reluctance is that it's different from C# and it doesn't have to be. We have to choose a default for each attribute - are you in a C# context or a text context. Every time an attribute is different, or unique, that's something that consumer have to learn about instead of just writing normal C#. I don't think that shorter necessarily means better. |
What's the semantical difference between assigning a string directly or having to use a "@" plus parentheses?
Sorry I don't get your point. What you say it is different from c# is exactly what Blazor is doing right now with string class to avoid gross syntax, as you described. You are contradicting your own argument.
I agree, but in this case I think the extra "@" and "()" doesn't give any advantage to the users, all the contrary it makes the razor language boilerplate unnecessarily. |
Right. Strings are special cased because the alternative is awful. If you want to use some other type that isn't a string, there is no extra ceremony required.
|
We both agree about this.
I think I'm starting to get your point. I thought the above didn't work unless an "@" would be used. I imagined that inserting a peace of c# inside html would require the "@" (and the lack of intelli-sense coloring for some cases made me confuse). Anyway I wonder if requiring the "@" to change context wouldn't be a better alternative to actual implementation. It would be more intuitive, the string case wouldn't be different from the rest, and it would allow enums and custom classes to be more easily specified. What do you think? |
We considered and rejected this because:
|
We're on the same page now. Thanks for taking time to explain this to me. |
Does TagHelpers work in Blazor? |
No, tag helpers are incompatible at a fundamental level. TagHelpers output text, Blazor is like an instruction language for markup. |
I'm having trouble to understand this argument
I wonder if this applies to Blazor anyhow. Regarding the other argument
I would feel rather natural to need to use the "@" in this case.
|
We want to be consistent and avoid two technologies that look the same with totally different semantics. |
I can understand this. But anyway I don't get what does this mean "TagHelpers already put you in a c# context" and how is this related to the fact that the component attributes are actually c# code even if the "@" is not specified? |
They are consistent today. Like I said, we considered a change and decided to do the same thing tag helpers do. |
This was the post that clarified to me how Blazor is working. I've had some time to think on this, and still think there could be a way to use TypeConverter. You said there is a function that determines whether the parameter type is a string and in that case assigns the string directly into the property, otherwise the attribute value is c#. My point is the function that determined the kind of content inside the attribute, could use https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.typeconverter.canconvertfrom?view=netframework-4.8 with typeof(string) to determine whether the attribute value is a plain string of c#. Wouldn't that work? |
Thanks for contacting us. |
I'm am trying to add a parameter of type "C1Color" I created to a razor component, and I'd like to pass it as a string.
Is there any way to use System.ComponentModel.TypeConverter for these scenarios?
I know I can do something like this
But I want to avoid it.
The text was updated successfully, but these errors were encountered: