2021年1月21日星期四

C# how to use a dictionary name which is inside a string [duplicate]

I know I should probably use Reflection but I didn't understand the solutions I found. I have 100 dictionaries. Let's say , dict1, dict2, dict3 etc. I have a string. MyString = "dict3" for example. How to use that string to access the dictionary.

Here should be the solution: string to variable name but I can't get it to work.

I tried

using System.Reflection;    var dictionaryToUse = (myString.GetType().GetProperty(Name));  // doesnt work  var result = DictionaryToUse[key];    var dictionaryToUse = typeof(myString).GetProperty(Name);      // Doesnt work either  var result = DictionaryToUse[key];  

I think my approach is wrong but can't figure out!

https://stackoverflow.com/questions/65839134/c-sharp-how-to-use-a-dictionary-name-which-is-inside-a-string January 22, 2021 at 12:03PM

没有评论:

发表评论