假设我有一个看起来像这样的类:
class Derived : // some inheritance stuff here
{
}
Run Code Online (Sandbox Code Playgroud)
我想在我的代码中检查这样的内容:
Derived is SomeType;
Run Code Online (Sandbox Code Playgroud)
但看起来像is运算符需要Derived是Dervied类型的变量,而不是Derived本身.我不想创建Derived类型的对象.
如何在SomeType不实例化的情况下确保Derived继承?
PS如果它有帮助,我想要一些where关键字与泛型有关的东西.
编辑:
类似于这个答案,但它正在检查一个对象.我想查看课程本身.
我有一个抽象类:
abstract class AbstractDataExport
{
public string name;
public abstract bool ExportData();
}
Run Code Online (Sandbox Code Playgroud)
我有从AbstractDataExport派生的类:
class XmlExport : AbstractDataExport
{
new public string name = "XmlExporter";
public override bool ExportData()
{
...
}
}
class CsvExport : AbstractDataExport
{
new public string name = "CsvExporter";
public override bool ExportData()
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?(伪代码:)
foreach (Implementation imp in Reflection.GetInheritedClasses(AbstractDataExport)
{
AbstractDataExport derivedClass = Implementation.CallConstructor();
Console.WriteLine(derivedClass.name)
}
Run Code Online (Sandbox Code Playgroud)
输出像
CsvExporter
XmlExporter
Run Code Online (Sandbox Code Playgroud)
?
这背后的想法是创建一个派生自AbstractDataExport的新类,这样我就可以自动遍历所有实现并将名称添加到Dropdown-List.我只是想编译派生类而不改变项目中的任何其他内容,重新编译,宾果游戏!
如果您有其他解决方案:告诉他们.
谢谢
我有一组类,每个类都可以使用外部应用程序打开不同类型的文件,并告诉该应用程序将文件打印到特定的打印机.这些类都继承了一个公共抽象类和一个接口.
internal interface IApplicationPrinter : IDisposable
{
string ApplicationExe { get; }
string ApplicationName { get; }
string[] PrintableExtensions { get; }
IApplicationPrinter CreateInstance(string Filename, string Printer);
void Print();
bool ExitApplicationAfterPrint { get; set; }
bool WaitApplicationExitOnPrint { get; set; }
System.IO.FileInfo PdfFile { get; protected set; }
}
internal abstract class ApplicationPrinter : IApplicationPrinter
{
...
}
internal class WordPrinter : ApplicationPrinter
{
internal static string[] PrintableExtensions { get { return new string[]{".doc", ".docx" }; } }
...
} …Run Code Online (Sandbox Code Playgroud)