Is there any limitations on the kind of characters that I can use to create a property for a dynamic object?
Is there a list of characters that I cannot use (e.g. / * @)?
Suppose you have:
dynamic eo = new ExpandoObject();
then clearly if you expand the object through C# syntax, you need to follow the C# rules, for example:
eo.ValidCSharpIdentifier = 42;
eo._OK = 42;
eo.æ = 42;
But if you expand eo
by casting to IDictionary<,>
, then you can use other characters:
((IDictionary<string, object>)eo).Add("NotValidC#Identifier", 42);
((IDictionary<string, object>)eo).Add("42", 42);
((IDictionary<string, object>)eo).Add("/", 42);
((IDictionary<string, object>)eo).Add("*", 42);
((IDictionary<string, object>)eo).Add("@", 42);
((IDictionary<string, object>)eo).Add("", 42);
As we see, strange identifiers are possible. The string must be an object though, so a null
reference cannot be used (as in .Add(null, 42) /* illegal */
).
There seems to be no restriction on the name used. The string keys seem to be compared with an ordinal string comparison, like in EqualityComparer<string>.Default
or StringComparer.Ordinal
.
Certainly, some .NET languages have different rules for valid characters in names, compared to the C# rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With