简体   繁体   中英

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.

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.

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# .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM