搜索类是否包含特定属性

我的项目中有一个设置文件,其中包含一些StringCollection(用户一个)。每个StringCollection都以用户名命名,并包含该用户的一个或多个值。例如,如果用户名为“User1”,则设置将包含以下内容:


<setting name="User1" serializeAs="Xml">

    <value>

        <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

            <string>mail1@test.com</string>

            <string>mail2@test.com</string>

        </ArrayOfString>

    </value>

</setting>

我想测试我的设置是否包含某个用户,即我想知道类Properties.Settings.Default是否有一个名为该用户的属性。我能怎么做?


如果我的问题不清楚,我想要这样的东西:


private bool UserExists(String user)

{

    // Test if a property in Properties.Settings.Default is named "user"

}

就我而言,我有一个字段Properties.Settings.Default.User1。UserExists("User1")必须如此而且True必须UserExists("User2")如此False


qq_花开花谢_0
浏览 89回答 3
3回答

哔哔one

您可以将设置加载为StringCollection并调用Contains()集合以确定您的用户是否存在例子private bool UserExists(String user){&nbsp; &nbsp; // Test if a StringCollection is named "user"&nbsp; &nbsp; return Properties.Settings.Default.User1.Contains(user);}编辑您要做的是查找设置是否存在,因为您可以尝试这样的事情private bool UserExist(string user){&nbsp; &nbsp;return Properties.Settings.Default.Properties.Cast<SettingsProperty>().Any(prop => prop.Name == user);}

肥皂起泡泡

或者你正在寻找这样的东西?private bool UserExists(String user){&nbsp; &nbsp;// Test if a StringCollection is named "user"&nbsp; &nbsp;return user == Properties.Settings.Default.name ;}

拉丁的传说

您的问题不清楚,您的澄清使问题变得混乱。您的标题是“搜索字符串文件是否包含字段”,然后您声明您想查看您的设置是否包含某个用户,而您的澄清似乎表明您想要一个集合的名称?由于该列表中的第一项和最后一项没有多大意义,我将回答第二项。根据StringCollection 的 MSDN 文档,扩展方法.Contains()完全符合您的要求。这是文档示例的修改版本,它更符合您的问题。using System;using System.Collections;using System.Collections.Specialized;public class SamplesStringCollection{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Creates and initializes a new StringCollection.&nbsp; &nbsp; &nbsp; &nbsp; StringCollection myCol = new StringCollection&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mail1@gmail.com",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mail2@gmail.com",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mail3@gmail.com",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mail4@gmail.com"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; if (UserExists(myCol, "mail2@gmail.com"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("User 2 exists!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static bool UserExists(IList myCol, string user)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return myCol.Contains(user);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP