简体   繁体   English

我如何从运行时已知的类派生一个类,实现编译时已知的接口

[英]How can I have a class derived from a class known at runtime implementing an interface known at compilation

I think I'd be easier to just write down my pseudo-class declaration. 我想写下自己的伪类声明会更容易。

Basically, I want something like 基本上,我想要类似

public class DatabaseControlsDAL<T> : T, IDatabaseControlsDAL where T : IBaseDAL
{
   public int myMethod()
   {
      return somePublicPropertyInTheIBaseDALInterface;
   }
}

But it won't work :( 但这是行不通的:(

Any help welcome ! 任何帮助欢迎!

Generics aren't a run-time feature, thus giving a generic argument won't be a run-time issue . 泛型不是运行时功能,因此提供泛型参数不会是运行时问题

In the other hand, how would a class inherit what will be only known at run-time? 另一方面,类将如何继承仅在运行时才知道的内容? Think C# is a strongly-typed language. 认为C#是一种强类型的语言。

If B inherits A, but A is "unknown", then, until run-time, B won't be of type A. This defeats both the point of inheritance and polymorphism. 如果B继承了A,但是A是“未知的”,那么直到运行时,B的类型都不会变为A。这会破坏继承点和多态性。

Conclusion: if you don't provide a background in order to let us define a better solution (well, an existing solution!), you'll get this answer: you can't achieve what you're looking for in C# . 结论:如果您没有提供背景知识来让我们定义一个更好的解决方案(嗯,一个现有的解决方案!),您将得到以下答案: 您将无法实现C#的目标

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在运行时创建(= compile)类,以实现已知接口 - create (=compile) class at runtime that implements known interface 从接口继承时如何使用派生类 - How can I use derived class when inherite from an interface 接口继承-从父接口隐藏方法-为什么在实现类中可以同时使用这两种方法? - Interface inheritance - hiding method from parent interface - why can I have both methods in the implementing class? 在基类和派生类中实现接口 - Implementing an interface in a base and derived class 实现定义基类属性的接口时,为什么类实现接口不能返回派生类类型对象? - When implementing an interface which define a base class property why can't the class implementing interface return a derived class type object? 如何从接口访问派生类成员? - How to access derived class members from an interface? 从实现已知接口的未知类型的构造函数创建委托 - Create delegate from a constructor of unknown type implementing a known interface 当我有IEnumerable时如何找到一个派生类实现 <T> 来自Ninject绑定 - How can i find one derived class implementation when i have IEnumerable<T> from Ninject binding 直到运行时才知道派生类的XML序列化 - Xml serialization of derived classes not known until runtime 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM