Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters for dynamic objects?

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. / * @)?

like image 766
Oliver Avatar asked May 08 '13 15:05

Oliver


1 Answers

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.

like image 91
Jeppe Stig Nielsen Avatar answered Oct 21 '22 04:10

Jeppe Stig Nielsen