2017-08-21 5 views
-3

私は3つのクラス、すなわち "Class1"、 "Class2"、 "Class3"と1つのインタフェースIBaseを持っています。デザインの提案 - プロパティ、インターフェイス(または抽象) - アクセスの問題を持つクラス

interface IBase 
{ 
    + PrpCommon {get;set;} 
} 

Class1:IBase 
{ 
+ PrpCommon {get;set;} 

+ Class1Prp {get;set;} (and few other properties) 
} 

Class2:IBase 
{ 
+ PrpCommon {get;set;} 
+ Class2Prp {get;set;} (and few other properties) 
} 

Class3:IBase 
{ 
    + PrpCommon {get;set;} 
    + Class3Prp {get;set;} } 

AccessClass 
{ 

    AccessFunction(IBase) 
{ 

    return IBase.PrpCommon;  
// here i need to access the other properties like Class3Prpr or Class2Prp or Class1Prp. 
} 
} 

MainClass 
{ 
    AccessClass.AccessFunction(new Class1) 
// Here need to access the other properties like Class3Prpr or Class2Prp or Class1Prp. 
} 

可能ですか?私はいくつかのコンストラクタを介してそれを聞いたことがありますが、インターフェイスにはアクセスできません。


答えて

0

私はこれを正しく理解していません。 AccessClass内の派生クラスのプロパティにアクセスしますか? その場合、基本クラスまたはAccessClass用のインターフェイス、IBaseクラスとAccessClassFactoryそれぞれの特定の実装を持つオプションがあります。このようなもの:

interface IAccessClass 
{ 
    AccessFunction<T>(T item) where T : IBase; 
} 

class AccessClass1 : IAccessClass<Class1>{ 
    AccessFunction(Class1 item){ 
     // here you can do whatever you want with the properties 
    } 
} 

class AccessClassFactory{ 
    IAccessClass<T> Create<T>() where T : IBase{ 
     var classType = typeof(T); 
     if(classType == typeof(Class1)) return new AccessClass1(); 
     // and so on 
    } 
} 

私はあなたがアイデアを得たと思う!

+0

ありがとう、私はこれを期待していた...もう一度ありがとう.... – kamal

関連する問題