0
public class NetworkCredentialHolder
{
    public NetworkCredentialHolder()
    {
        _networkCred = new NetworkCredential();
    }

    public string UserName
    {
        get { return _networkCred.UserName; }
        set { _networkCred.UserName = value; }
    }

    public string Domain
    {
        get { return _networkCred.Domain; }
        set { _networkCred.Domain = value; }
    }
}

public class SPS2003NetworkConfiguration
{
public SPS2003NetworkConfiguration()
    {
        _targetCredentials = new NetworkCredentialHolder();
    }

    private NetworkCredentialHolder _targetCredentials;
}

class Program
{
    static void Main(string[] args)
    {
        var netConfiguration = new SPS2003NetworkConfiguration();
        netConfiguration.SetGlobalCredentials("sanz", "pass@word1TestPassed");

        var sb = new StringBuilder();
        var writer = new StringWriter(sb);

        XmlSerializer serializer = new XmlSerializer(typeof(SPS2003NetworkConfiguration));
        serializer.Serialize(writer, netConfiguration);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(sb.ToString());

        XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
        XmlSerializer serializer = new XmlSerializer(typeof(SPS2003NetworkConfiguration));

        object obj = serializer.Deserialize(reader);

        var netConfiguration = (SPS2003NetworkConfiguration)obj;
        var encryptedPwd =      netConfiguration.TargetCredentials.SecurePassword;
    }
}

嗨,我在这里创建了

private NetworkCredentialHolder _targetCredentials;

当我创建_targetCredntials为私有时,NetworkCrendtialHolderlikeUserName下定义的属性Domain不会序列化,除非_targetCredentials定义为公共。有什么办法可以序列化UserNameDomain同时保持_targetCredential私有?

4

0 回答 0