开发者

C# Parent Class with interface based properties that can change per subclassed declaration

I've looked around but can't get my head around what I'm trying to do. I want to create a baseline interface that multiple other classes can be derived from. No problem. Then, I want another class that has a property on it of the common interface base, but not guaranteeing which subclassed version of the interface it will be using. What approach should I use. Here's some example of what I'm trying to simulate.

public interface IB开发者_Python百科aseline
{
    string CommonToAll { get; }
    int AnotherCommon { get; }

    void CommonFunction();
}

Now, deriving new classes from interface IBaseline

public class CDerived1 : IBaseline
{
    private string CommonToAll
    { get { return Whatever; }}

    private int AnotherCommon
    { get { reutrn WhateverInt; }}

    public void CommonFunction)
    { // do something with this classes specific elements...  }


    // now, here are some custom things specific to this class
    private string CustomToThisClass;

    private int CustomFunction()
    { // do something }
}

public class CDerived2: IBaseline
{
    private string CommonToAll
    { get { return Whatever; }}

    private int AnotherCommon
    { get { reutrn WhateverInt; }}

    public void CommonFunction)
    { // do something with this classes specific elements...  }

    // again, these are totally different to the second derived class
    private string TotallyDifferent;

    private bool DiffFunction( int anyParm )
    {  // do something different }

}

Now, for my confusion to implement. I want to create a new class that has a field on it that can be of ANY IBaseline structure, but don't know which version at compile time.

public class OtherClass
{
   IBaseline GenericInstance;
   ...
   ...

   public void CommonAccessFunction()
   {
      // call the common function that is generic no matter WHAT 
      // subclassed instance is used.
      GenericInstance.CommonFunction();
   }
}


Make it generic:

public class OtherClass<T> where T : IBaseline
{
    public T GenericInstance { get; private set;}

    public OtherClass(T genericInstance)
    {
        this.GenericInstance = genericInstance;
    }
}


why can't you just pass an instance of IBaseLine into OtherClass, either via the constructor (like in the example below) or via a property?

public class OtherClass
{
   IBaseline GenericInstance;

   public OtherClass(IBaseline instance)
   {
        GenericInstance=instance;
   }

   public void CommonAccessFunction()
   {
      // call the common function that is generic no matter WHAT 
      // subclassed instance is used.
      GenericInstance.CommonFunction();
   }
}

either I'm missing something or your question is not very clear. It seems that what you want to do is something that standard practice, your implementation of OtherClass doesn't know anything about which implementation of IBaseline it is using, just that it is using some implementation...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜