我的代码:

public class Contact
{
    public string id{ get; set; }
    public string contact_type_id { get; set; }
    public string value{ get; set; }
    public string person_id { get; set; }
    public Contact()
    {

    }
}

public class Contact:Base.Contact
{
    public ContactType ContactType { get; set; }
    public Person Person {get; set;}
    public Contact()
    {
        ContactType = new ContactType();
        Person = new Person();
    }
}

Contact c = new Contact();
Base.Contact cb = (Base.Contact)c;

在问题:

The **cb** is set to **Contac** and not to **Base.Contact**.
Have any trick to do that????
有帮助吗?

解决方案

我读的答案,还是不明白的问题! 什么是与上面的代码的问题吗? 点击搜索结果 此外,从意见,据我所知,你只需要序列化的基类。我认为是没有问题的,开始的时候。看例如 -

class A
{
    public int a = -1;
};

class B : A
{
    public int b = 0;
};

class Program
{
    static void Main(string[] args)
    {
        A aobj = new B();
        aobj.a = 100; // <--- your aobj obviously cannot access B's members.
        Console.In.ReadLine();
    }
}

现在,如果你必须使你的序列化功能虚拟化,然后是的,有问题的。那么这可能帮助 -

abstract class Ia
{
    public abstract void Serialize();
}
class A : Ia
{
    public int a = -1;
    sealed public override void Serialize() {
        Console.Out.WriteLine("In A serialize");
    }
};

class B : A
{
    public int b = 0;
    /*
     * Error here -
    public override void Serialize()
    {
        Console.Out.WriteLine("In B serialize");
    }
     */

    //this is ok. This can only be invoked by B's objects.
    public new void Serialize()
    {
        Console.Out.WriteLine("In B serialize");
    }
};

class Program
{
    static void Main(string[] args)
    {
        A aobj = new B();
        aobj.Serialize();
        Console.In.ReadLine();
    }
}

//Output: In A serialize

其他提示

无关的Silverlight。

这是什么铸造呢 - 你还是返回一个参考c,其中的 Base.Contact

您不能调用ContactTypePersoncb(除非你上溯造型,你不应该)。

什么问题与是?

您绝对无法通过浇铸转一类到它的基类中的一个。根据您所使用的序列化的类型,你可以告诉串行承担类是基本类型,这将限制序列化基础类型的成员。

添加复制构造成基类(例如public Contact(Contact other)),然后就可以做到这一点:

Contact c = new Contact();
Base.Contact cb = new Base.Contact(c);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top