我有两个代码相似但有不同变量
的 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);
}
}
}
现在我想创建一个抽象类并将我的公共代码放在该类文件中,然后在这两个类文件中继承该代码
答案 0 :(得分: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 内部,那么你可以使用封装而不是继承 - 这仍然是重用代码,但管道是不同的。
答案 1 :(得分: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;
}
}
}