2012-03-24 105 views
0

我有一個具有我的另一個類B的對象的類A.類B具有可以是任何數據類型的屬性。這裏是我的更改屬性的數據類型

public class A : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    object value; 
    int max; 
    string dataType; 
    bool nullable; 
    bool isKey; 
    bool isIdentity; 
} 

現在另一個B類是這樣

public class B : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public B() 
    { 
     A objA=new A(); 
    } 
} 

現在,在我的代碼,我將實例B的對象,不知何故,我想重寫objA的一些屬性值數據類型,例如字符串或int。我不想在我的代碼中對它進行類型轉換,我想在類B中對它進行類型轉換,因爲我將知道它在類B中的數據類型。

另外,如果有人能告訴我更好的方法做這個。

感謝&問候, 普山

+0

是'objA'在B中的場?如果不是,那麼當B構造函數完成時它將超出範圍。 – psubsee2003 2012-03-24 10:31:24

回答

4

您可以創建通用的A類,並選擇其類型B中實例化時:

public class A<T> : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    T value; 
    int max; 
    string dataType; 
    bool nullable; 
    bool isKey; 
    bool isIdentity; 
} 

public class B : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public B() 
    { 
     A<int> objA = new A<int>(); 
    } 
}