C# 具有空值的自动属性

C# 具有空值的自动属性,c#,C#,我有以下问题 我的自动属性SearchedObjClass、SearchedProp、SearchedPropValue结果为空值,尽管我在主程序中为它们赋值: 有人能帮我找出问题所在吗: class ADClassNew { public static DirectoryEntry createDirectoryEntry() { string ldapusername = "Username"; string ldapuserp

我有以下问题

我的自动属性SearchedObjClass、SearchedProp、SearchedPropValue结果为空值,尽管我在主程序中为它们赋值: 有人能帮我找出问题所在吗:

class ADClassNew
{
    public static DirectoryEntry createDirectoryEntry()
    {
            string ldapusername = "Username";
            string ldapuserpass = "Password";

        using (DirectoryEntry root =new DirectoryEntry())
        {
            ADClassNew adclass = new ADClassNew();
            root.Path = adclass.LdapPath;
            root.Username = ldapusername;
            root.Password = ldapuserpass;
            root.AuthenticationType = AuthenticationTypes.Secure;
            return root;
        }


     }

    public string SearchedObjClass { get; set; }
    public string SearchedProp { get; set; }
    public string SearchedPropValue { get; set; }
    public string LdapPath { get; set; }
    public StringCollection LoadProperties { get; set; }

    public SearchResult searchDirectory()
    {

        DirectoryEntry searchEntry = ADClassNew.createDirectoryEntry();
        DirectorySearcher search = new DirectorySearcher();
        search.SearchRoot = searchEntry;
        ADClassNew adclassnew = new ADClassNew();

       //string _searchedObjClass = SearchedObjClass;
       //string _searchedProp = SearchedProp;
       //string _searchedPropValue = SearchedPropValue;

       search.Filter = string.Format("(&(ObjectClass={0})({1}={2}))", adclassnew.SearchedObjClass, adclassnew.SearchedProp, adclassnew.SearchedPropValue);
       //search.Filter = "(&(objectClass=user)(cn=administrator))";
       search.PropertiesToLoad.Add("memberof");
           SearchResult result = search.FindOne();
       return result;

    }
}


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ADClassNew adclassnew = new ADClassNew();
        adclassnew.LdapPath = "LDAP://MyDomain";
        adclassnew.SearchedObjClass = "User";
        adclassnew.SearchedProp = "Displayname";
        adclassnew.SearchedPropValue = "administrator";
    }
}
您不需要对表单构造函数中创建的对象执行任何操作—它只是超出范围并将被收集。您是否希望值在
ADClassNew
的所有实例中保持不变?如果是,则使用
static
属性:

public static string SearchedObjClass { get; set; }
public static string SearchedProp { get; set; }
public static string SearchedPropValue { get; set; }
public static string LdapPath { get; set; }
然后在初始化中使用类名而不是实例设置它们:

ADClassNew.LdapPath = "LDAP://MyDomain";
ADClassNew.SearchedObjClass = "User";
ADClassNew.SearchedProp = "Displayname";
ADClassNew.SearchedPropValue = "administrator";
或者,您可以将该对象设置为窗体的属性以重复使用它:

public partial class Form1 : Form
{
    private ADClassNew _adClassNew {get; set;}

    public Form1()
    {
        InitializeComponent();

        _adclassnew = new ADClassNew();
        _adclassnew.LdapPath = "LDAP://MyDomain";
        _adclassnew.SearchedObjClass = "User";
        _adclassnew.SearchedProp = "Displayname";
        _adclassnew.SearchedPropValue = "administrator";
    }
}

您在哪里检查它是否为空?