C# 除了从密封类继承之外,还有其他方法吗?
我问这个问题的原因是因为我想创建一个具有FileInfo类(从FileInfo派生)所有功能的类,并允许我向其中添加自己的属性 我认为一个例子会更有效地合作。 我想要的是:C# 除了从密封类继承之外,还有其他方法吗?,c#,wpf,inheritance,sealed,C#,Wpf,Inheritance,Sealed,我问这个问题的原因是因为我想创建一个具有FileInfo类(从FileInfo派生)所有功能的类,并允许我向其中添加自己的属性 我认为一个例子会更有效地合作。 我想要的是: BindingList<FileInformation> files = new BindingList<FileInformation>(); public void GatherFileInfo(string path) { files.Add(new FileInformation(pa
BindingList<FileInformation> files = new BindingList<FileInformation>();
public void GatherFileInfo(string path)
{
files.Add(new FileInformation(path));
listboxFiles.DataContext = files;
}
class FileInformation : FileInfo
{
public bool selected = false;
}
BindingList文件=新的BindingList();
public void GatherFileInfo(字符串路径)
{
添加(新文件信息(路径));
listboxFiles.DataContext=文件;
}
类FileInformation:FileInfo
{
所选公共布尔=假;
}
与我担心的相比,我必须做:
BindingList<FileInformation> files = new BindingList<FileInformation>();
public void GatherFileInfo(string path)
{
files.Add(new FileInformation(path));
listboxFiles.DataContext = files;
}
class FileInformation : FileInfo
{
string path = "<whatever>"
FileInfo fileInfo = new FileInfo(path);
public bool selected = false;
public string Name
{
get { return fileInfo.Name }
}
//Manually inherit everything I need???
}
BindingList文件=新的BindingList();
public void GatherFileInfo(字符串路径)
{
添加(新文件信息(路径));
listboxFiles.DataContext=文件;
}
类FileInformation:FileInfo
{
字符串路径=“”
FileInfo FileInfo=新的FileInfo(路径);
所选公共布尔=假;
公共字符串名
{
获取{return fileInfo.Name}
}
//手动继承我需要的一切???
}
这样做的好处是,在WPF中,您可以简单地绑定到类FileInformation的所有属性,包括继承的FileInfo类的属性
我从来没有研究过这件事,也没有什么线索可以告诉我应该从哪里开始研究,所以举个例子或线索来说明如何做会很有帮助。在.Net中确实没有办法从一个密封的类继承。您可以编写扩展方法,但这不允许您添加新属性或字段。您唯一可以做的另一件事是模拟继承,但创建您自己的类,其中包含您要从中继承的类类型的字段,然后通过为每个类编写包装器方法手动公开“基类”的每个属性和方法。小班倒不坏,大班倒是很痛苦
我已经编写了代码生成器程序来使用反射自动完成这项工作。然后我得到它的输出并扩展它。但这不是真正的继承。我个人不喜欢密封类的概念,因为它会阻止扩展这些类。但我认为他们这样做是出于性能原因。由于
FileInfo
继承自MarshalByRefObject
,您可以创建一个自定义代理,它模仿FileInfo
,并在您自己的实现中处理所有调用。但是,您将无法强制转换该类,更重要的是,您无法使用自定义属性扩展此类。无论如何,如果有其他人想要这个,请使用一些工具来帮助它。要从密封类继承,请尝试使用装饰设计模式,
基本思想是创建OldClass的私有实例,并手动实现其方法,如:
public class NewClass
{
private OldClass oldClass = new OldClass();
public override string ToString()
{
return oldClass.ToString();
}
//void example
public void Method1()
{
oldClass.Method1();
}
//primitive type example
public int Method2()
{
return oldClass.Method2();
}
//chaining example, please note you must return "this" and (do not return the oldClass instance).
public NewClass Method3()
{
oldClass.Method3();
return this;
}
}
public class Demo
{
static void Main(string[] args)
{
var newClass = new NewClass();
newClass.Method3();
WriteLine(newClass);
}
}
为什么不能通过公共属性公开嵌套在
FileInformation
类中的FileInfo
对象,而不是逐个声明其属性的包装器?这是一种可能性,但是为了代码的可读性,我想知道在没有遍历对象绑定到属性(如:FileI.FullName
)的情况下,这是否可能。这也不总是最佳实践。你不应该为了代码可读性而从类继承。如果有什么是有害的,在同一个你试图使用它。此外,“Selected”是您在UI上执行的一些操作,它可能不应该存在于模型中。听起来您需要一个视图模型,就像在MVVM模式中一样FileInfo
是您的模型,WPF XAML是您的视图;它们之间缺少一个视图模型。我确实实现了MVVM。我将使用选定的属性名称,以便将其存储在数据库中。重新启动应用程序将保留选定的所有项目。作为奖励,我可以将该属性绑定到复选框。有更好的办法吗?这就是我害怕的。。。太糟糕了,我放弃了寻找懒散的解决方案。你完全正确。