Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 避免Application.Current.Properties硬编码键的最佳方法是什么?_C#_Xamarin.forms_.net Standard - Fatal编程技术网

C# 避免Application.Current.Properties硬编码键的最佳方法是什么?

C# 避免Application.Current.Properties硬编码键的最佳方法是什么?,c#,xamarin.forms,.net-standard,C#,Xamarin.forms,.net Standard,在我的Xamarin.Forms应用程序中,我添加了一个.NET标准库,我想在其中使用Properties字典 但是,我希望避免硬编码键“id” 最好的方法是什么?可能没有最好的方法,但有一种方法是使用nameof语法,因为如果重命名属性,这也会发生更改: // I added ToLowerInvariant, because you wrote "id". Application.Current.Properties[nameof(someClass.ID).ToLowerInvariant

在我的Xamarin.Forms应用程序中,我添加了一个.NET标准库,我想在其中使用Properties字典

但是,我希望避免硬编码键“id”


最好的方法是什么?

可能没有最好的方法,但有一种方法是使用nameof语法,因为如果重命名属性,这也会发生更改:

// I added ToLowerInvariant, because you wrote "id".
Application.Current.Properties[nameof(someClass.ID).ToLowerInvariant()] = someClass.ID;

可能没有最好的方法,但有一种方法是使用nameof语法,因为如果重命名属性,这也会发生更改:

// I added ToLowerInvariant, because you wrote "id".
Application.Current.Properties[nameof(someClass.ID).ToLowerInvariant()] = someClass.ID;

我将为此创建一个静态类,沿着

public static class Constants
    {
        public const string Id = "id";
        // etc...
    }
然后将代码更新为

Application.Current.Properties [Constants.Id] = someClass.ID;

我将为此创建一个静态类,类似于

public static class Constants
    {
        public const string Id = "id";
        // etc...
    }
然后将代码更新为

Application.Current.Properties [Constants.Id] = someClass.ID;

我的回答是针对你的问题吗?我不确定,如果你问我提供了什么。是的,@Nikolaus谢谢你。一个被接受的答案会针对我的需要。;-)我的回答是针对你的问题吗?我不确定,如果你问我提供了什么。是的,@Nikolaus谢谢你。一个被接受的答案会针对我的需要。;-)