I am writing a library, and I have a method which takes in a Dictionary. The value of the dictionary is untrusted/insecure, but the key is trusted and if an end-user were given the ability to enter an arbitrary key name then "bad things" could happen.
So when other developers use this library function, I want to force them to know the key name at compile time. So something like this would be allowed:
string userInput = Console.ReadLine();
Dictionary<string, string> something = new Dictionary<string, string>();
something.Add("MyKey", userInput);
Because "MyKey" is a string literal, known at compile time. But something like this would either raise a compile or runtime exception:
string userInput = Console.ReadLine();
string userKey = Console.ReadLine();
Dictionary<string, string> something = new Dictionary<string, string>();
something.Add(userKey, userInput);
Because user input was used for the key (userKey) and thus it was unknown at compile time.
I've looked in GetType() and there is nothing that really distinguishes between a literal string and a string created at runtime.