更改属性的数据类型

时间:2012-03-24 10:24:35

标签: c# properties

我有一个A类,它有另一个B类的对象.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的属性VALUE覆盖为某些数据类型,例如string或int。我不想在我的代码中对它进行类型转换,我想在类B中对它进行类型转换,因为我将知道它在B类中的数据类型。

此外,如果有人能告诉我更好的方法,我将不胜感激。

谢谢&问候, 普山

1 个答案:

答案 0 :(得分: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>();
    }
}
相关问题