jeudi 30 juillet 2015

Setting Property using only string name

Is there a way I can set a property by identifying it using a string?

For example, I have a Visibility property that looks something like this:

public Visibility ModifyFilesIconVisibility
        {
            get { return modifyFilesIconVisibility; }
            set
            {
                SetProperty(ref modifyFilesIconVisibility, value, () => modifyFilesIconVisibility);
            }
        }

which is bound to an icon in XAML. Since each icon's visibility is set during runtime based on user authority to access each APIs, I have a Dictionary mapping:

public static Dictionary<string, List<string>> Views = new Dictionary<string, List<string>> {
                        {
    "ModifyFiles", 
            new List<string>{"/editFile", "/deleteFile", "/cutFile", "/copyFile"}
        }, 
                        {
    "CRUDLogs", 
            new List<string>{"/writeLog", "/deleteLog", "/viewLog", "/searchLog"}
        },       
                        };    

and if any of the APIs in the List is available in the authority (which I receive from an external API as well), I will modify the visibility of each icon. So for example, if /editFile is available to the user, the ModifyFilesIconVisibility will be set:

foreach (string api in APIMappings.Views["ModifyFiles"])
{
  if (URLs.Contains(api)) 
  {
   this.ModifyFilesIconVisibility = Visibility.Visible;
   break;
  }
}

Otherwise they are left as Visibility.Collapsed.

Since this is tedious, I was wondering if I can somehow use the name of the property itself in the mapping

public static Dictionary<string, List<string>> Views = new Dictionary<string, List<string>> {
                        {
    "ModifyFilesIconVisibility", 
            new List<string>{"/editFile", "/deleteFile", "/cutFile", "/copyFile"}
        }, 
                        {
    "CRUDLogsIconVisibility", 
            new List<string>{"/writeLog", "/deleteLog", "/viewLog", "/searchLog"}
        },       
                        };    

or something like that, and then use the Dictionary key to set it to visible using reflection or anything else. Is this possible?





Aucun commentaire:

Enregistrer un commentaire