相关疑难解决方法(0)

Serialize包含Dictionary成员的Class

扩展我之前的问题,我决定(de)序列化我的配置文件类,它运行得很好.

我现在想存储驱动器盘符映射关联数组(关键是驱动器号,值是网络路径)和使用都试过Dictionary,HybridDictionaryHashtable打电话时的这一点,但我总是得到下面的错误ConfigFile.Load()或者ConfigFile.Save():

反映类型'App.ConfigFile'时出错.[snip] System.NotSupportedException:无法序列化成员App.Configfile.mappedDrives [snip]

从我读过的词典和HashTables可以被序列化,所以我做错了什么?

[XmlRoot(ElementName="Config")]
public class ConfigFile
{
    public String guiPath { get; set; }
    public string configPath { get; set; }
    public Dictionary<string, string> mappedDrives = new Dictionary<string, string>();

    public Boolean Save(String filename)
    {
        using(var filestream = File.Open(filename, FileMode.OpenOrCreate,FileAccess.ReadWrite))
        {
            try
            {
                var serializer = new XmlSerializer(typeof(ConfigFile));
                serializer.Serialize(filestream, this);
                return true;
            } catch(Exception e) {
                MessageBox.Show(e.Message);
                return false;
            }
        }
    }

    public void addDrive(string …
Run Code Online (Sandbox Code Playgroud)

.net c# serialization dictionary

135
推荐指数
7
解决办法
15万
查看次数

NameValueCollection到URL查询?

我知道我能做到这一点

var nv = HttpUtility.ParseQueryString(req.RawUrl);
Run Code Online (Sandbox Code Playgroud)

但有没有办法将其转换回网址?

var newUrl = HttpUtility.Something("/page", nv);
Run Code Online (Sandbox Code Playgroud)

asp.net url namevaluecollection url-encoding

69
推荐指数
7
解决办法
4万
查看次数

C#:将Dictionary <>转换为NameValueCollection

如何将a转换Dictionary<string, string>NameValueCollection

我们项目的现有功能返回了一个老式的NameValueCollection,我用LINQ修改.结果应该作为一个传递NameValueCollection.

我想以通用的方式解决这个问题.任何提示?

c# namevaluecollection

30
推荐指数
3
解决办法
2万
查看次数

从分隔字符串填充Dictionary <String,String>的最"优雅"方式

我认为那些对基本字符串操作,循环和字典有轻微把握的人可以解决如何从字符串填充字典,如下所示:

黑色:#00000 |绿:#008000 | (其中"黑色"是键,"#000000"是值)

但是在你看来,最优雅的做法是什么?我可以使用哪种最有效/更简洁的编码来实现它?到目前为止,我有:

    public static Dictionary<String, String> ThemeColors
    {
        get
        {
            Dictionary<String, String> themeColors = new Dictionary<string, string>();
            foreach (String colorAndCode in GetSettingByName("ThemeColors").ToString().Split('|'))
            {
                themeColors.Add(colorAndCode.Split(':').First(), colorAndCode.Split(':').Last());
            }
            return themeColors;
        }
    }
Run Code Online (Sandbox Code Playgroud)

GetSettingByName("ThemeColours")返回上面的字符串(以粗体显示).

它的功能显而易见,一切正常,但我想确保我现在开始思考这个问题并制定最好的做事方式而不仅仅是让它发挥作用.

我可以在Dictionary循环上使用yield吗?

c# string dictionary split

4
推荐指数
1
解决办法
1万
查看次数