-1

我有两个代码相似但变量不同的类

ISource.cs

public interface ISource
{
    string AvailConfigPath { get; }

    string AvailVersion { get; }

    IDictionary<string, string> AvailFiles { get; }
}

public class Source : ISource
{
    public string AvailConfigPath
    {
        get
        {
            return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config";
        }
    }

    private XDocument document = XDocument.Load(@"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config");

    public string AvailVersion
    {
        get
        {
            return document.Root
                         .Element("InfoConfigFile")
                         .Attribute("version").Value.ToString();
        }
    }

    public IDictionary<string, string> AvailFiles
    {
        get
        {
            return document.Root
                         .Element("files")
                         .Elements("file")
                         .ToDictionary(x => x.Attribute("name").Value,
                                       x => x.Attribute("version").Value);
        }
    }
}

ITarget.cs

   public interface ITarget
{
    string LocalConfigPath { get; }

    string LocalVersion { get; }

    IDictionary<string, string> LocalFiles { get; }
}

internal class Target : ITarget
{
    public string LocalConfigPath
    {
        get
        {
            return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
        }
    }

    private XDocument document = XDocument.Load(@"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config");

    public string LocalVersion
    {
        get
        {
            return document.Root
                     .Element("InfoConfigFile")
                     .Attribute("version").Value;
        }
    }

    public IDictionary<string, string> LocalFiles
    {
        get
        {
            return document.Root
                     .Element("files")
                     .Elements("file")
                     .ToDictionary(x => x.Attribute("name").Value,
                                   x => x.Attribute("version").Value);
        }
    }
}

现在我想创建一个抽象类并将我的公共代码放在那个类文件中,然后在这两个类文件中继承该代码

4

2 回答 2

3

看起来你应该能够以这种方式组合它们:

public interface ITarget
{
    string LocalConfigPath { get; }
    string LocalVersion { get; }
    IDictionary<string, string> LocalFiles { get; }
}

public interface ISource
{
    string AvailConfigPath { get; }
    string AvailVersion { get; }
    IDictionary<string, string> AvailFiles { get; }
}

internal abstract class BaseClass
{
    public virtual string ConfigPath { get; }
    private XDocument document = XDocument.Load(ConfigPath);

    public string Version
    {
        get
        {
            return document.Root
                     .Element("InfoConfigFile")
                     .Attribute("version").Value;
        }
    }

    public IDictionary<string, string> Files
    {
        get
        {
            return document.Root
                     .Element("files")
                     .Elements("file")
                     .ToDictionary(x => x.Attribute("name").Value,
                                   x => x.Attribute("version").Value);
        }
    }
}

internal class Target : BaseClass, ITarget
{
    public override string LocalConfigPath
    {
        get
        {
            return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
        }
    }

    public string LocalVersion
    {
        get { return Version; }
    }

    public IDictionary<string, string> LocalFiles
    {
        get { return Files; }
    }
}

public class Source : BaseClass, ISource
{
    public override string AvailConfigPath
    {
        get
        { return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config"; }
    }

    public string AvailVersion
    {
        get { return Version; }
    }

    public IDictionary<string, string> AvailFiles
    {
        get { return Files; }
    }
}

注意:不要使用类名BaseClass- 使其更适合域。我只是用它来说明那是“基类”

另请注意,如果Source是公开的,那么BaseClass也必须是公开的。如果你真的想在内部烘焙BaseClass,那么你可以使用封装而不是继承——这仍然是重用代码,但管道是不同的。

于 2013-06-19T13:59:25.807 回答
0

感谢 D Stanley__ 你的回答帮助我解决了这个问题

AbstractClass.cs

public abstract class BaseClass
{

    private string _ConfigPath;

    public string ConfigPath
    {
        get { return _ConfigPath; }
        set { _ConfigPath = value; }
    }


    private XDocument _Document = null;

    private XDocument document
    {
        get
        {
            if (_Document == null)
                _Document = XDocument.Load(ConfigPath);

            return _Document;
        }
    }

    public string Version
    {
        get
        {
            return document.Root
                     .Element("InfoConfigFile")
                     .Attribute("version").Value;
        }
    }

    public IDictionary<string, string> Files
    {
        get
        {
            return document.Root
                     .Element("files")
                     .Elements("file")
                     .ToDictionary(x => x.Attribute("name").Value,
                                   x => x.Attribute("version").Value);
        }
    }
}



ISource.cs

 public interface ISource
{
    string ConfigPath { get; }

    string AvailVersion { get; }

    IDictionary<string, string> AvailFiles { get; }
}
public class Source : BaseClass, ISource
{
    public Source()
    {
        ConfigPath = @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config";
    }


    public string AvailVersion
    {
        get
        {
            return Version;
        }
    }

    public IDictionary<string, string> AvailFiles
    {
        get
        {
            return Files;
        }
    }
}



ITarget.cs

public interface ITarget
{
    string ConfigPath { get; }

    string LocalVersion { get; }

    IDictionary<string, string> LocalFiles { get; }
}

internal class Target : BaseClass, ITarget
{
    public Target()
    {
        ConfigPath = @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
    }
    public string LocalVersion
    {
        get
        {
            return Version;
        }
    }

    public IDictionary<string, string> LocalFiles
    {
        get
        {
            return Files;
        }
    }
}
于 2013-06-20T03:04:04.530 回答