减少c#类中的重复代码

时间:2011-03-18 23:57:26

标签: c# class wrapper

这是我正在研究的API的包装器,我做得对吗?我不是特别喜欢构造函数中的所有重复代码,如果有人可以告诉我是否可以减少它会非常有用!

public class WebWizForumVersion
{
    // Properties of returned data
    public string Software { get; private set; }
    public string Version { get; private set; }
    public string APIVersion { get; private set; }
    public string Copyright { get; private set; }
    public string BoardName { get; private set; }
    public string URL { get; private set; }
    public string Email { get; private set; }
    public string Database { get; private set; }
    public string InstallationID { get; private set; }
    public bool NewsPad { get; private set; }
    public string NewsPadURL { get; private set; }

    public WebWizForumVersion(XmlReader Data)
    {
        try
        {
            Data.ReadToFollowing("Software");
            this.Software = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Version");
            this.Version = Data.ReadElementContentAsString();
            Data.ReadToFollowing("ApiVersion");
            this.APIVersion = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Copyright");
            this.Copyright = Data.ReadElementContentAsString();
            Data.ReadToFollowing("BoardName");
            this.BoardName = Data.ReadElementContentAsString();
            Data.ReadToFollowing("URL");
            this.URL = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Email");
            this.Email = Data.ReadElementContentAsString();
            Data.ReadToFollowing("Database");
            this.Database = Data.ReadElementContentAsString();
            Data.ReadToFollowing("InstallID");
            this.InstallationID = Data.ReadElementContentAsString();
            Data.ReadToFollowing("NewsPad");
            this.NewsPad = bool.Parse(Data.ReadElementContentAsString());
            Data.ReadToFollowing("NewsPadURL");
            this.NewsPadURL = Data.ReadElementContentAsString();
        }
        catch (Exception e)
        {

        }
    }
}

5 个答案:

答案 0 :(得分:4)

var properties = new [] {
    new {Name = "Software", Setter = new Action<string>(value => this.Software = value)},
    new {Name = "Version", Setter = new Action<string>(value => this.Version= value)},
    new {Name = "ApiVersion", Setter = new Action<string>(value => this.ApiVersion = value)},
    // ...
    new {Name = "NewsPad", Setter = new Action<string>(value => this.NewsPad = bool.Parse(value))},
}

foreach (var property in properties)
{
    Data.ReadToFollowing(property.Name);
    property.Setter(Data.ReadElementContentAsString());
}

答案 1 :(得分:4)

我将单独留下本地属性的赋值,并使用辅助方法从XML中读取值。

public class WebWizForumVersion
{
    public WebWizForumVersion(XmlReader Data)
    {
        this.Software = Data.ReadString("Software");
        this.Version = Data.ReadString("Version");
        this.APIVersion = Data.ReadString("ApiVersion");
        this.NewsPad = Data.ReadBool("NewsPad");
    }
}

public static class XmlReaderHelpers
{
    private string ReadString(this XmlReader Data, string name)
    {
        Data.ReadToFollowing(name);
        return Data.ReadElementContentAsString();
    }

    private bool ReadBool(this XmlReader Data, string name)
    {
        return bool.Parse(Data.ReadString(name));
    }
}

答案 2 :(得分:1)

您可以在Dictionary类型的对象中拥有所有值吗?所以这样的事情会简化事情:

public Dictionary<string, string> ForumVars = null;

public WebWizForumVersion(XmlReader Data)
{
    ForumVars = new Dictionary<string, string>();
    ForumVars.Add("Software", GetValue("Software"));
    ForumVars.Add("Version", GetValue("Version"));
    ForumVars.Add("APIVersion", GetValue("APIVersion"));
}

protected string GetValue(string key)
{
    Data.ReadToFollowing(key);
    return Data.ReadElementContentAsString();
}

我知道并非所有内容都可能是字符串(例如NewsPad),因此您可以使用动态或对象。

答案 3 :(得分:0)

是的,当使用字典时,一种简化它的方法就是这样。

new Dictionary<string, string>() {
    {"key", "val"},
    ...
}

答案 4 :(得分:0)

IF 您可以将InstallationID属性的名称更改为InstallID以匹配元素名称,然后您可以使用refelction:

public class WebWizForumVersion
{
    // Properties of returned data
    public string Software { get; private set; }
    public string Version { get; private set; }
    public string APIVersion { get; private set; }
    public string Copyright { get; private set; }
    public string BoardName { get; private set; }
    public string URL { get; private set; }
    public string Email { get; private set; }
    public string Database { get; private set; }
    public string InstallID { get; private set; }  // changed property name
    public bool NewsPad { get; private set; }
    public string NewsPadURL { get; private set; }

    public WebWizForumVersion( XmlReader Data )
    {
        try
        {
            PropertyInfo[] props = this.GetType().GetProperties();

            foreach( PropertyInfo pi in props )
            {
                Data.ReadToFollowing( pi.Name );
                if( pi.PropertyType == typeof( bool ) )
                {
                    pi.SetValue( this, bool.Parse( Data.ReadElementContentAsString() ), null );
                }
                else
                {
                    pi.SetValue( this, Data.ReadElementContentAsString(), null );
                }
            }
        }
        catch( Exception e )
        {
          // do something with exception
        }
    }
}