【问题标题】:Casting a object to a base class , return the extented object?将对象转换为基类,返回扩展对象?
【发布时间】:2010-04-13 19:12:26
【问题描述】:

我的代码:

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????

【问题讨论】:

    标签: c# silverlight casting extends


    【解决方案1】:

    与 Silverlight 无关。

    这就是强制转换的作用——你仍然返回一个对c的引用,它一个Base.Contact

    你不能在cb 上打电话给ContactTypePerson(除非你上当,你不应该这样做)。

    这有什么问题?

    【讨论】:

    • 我只需要实现我的基类,这是我的问题!!
    • DataContractJsonSerializer 做了一个深度序列化器,但我只需要 Base.Contact 上的属性。
    • 我需要一个 DownCast,但是当我将对象向下投射时,我的 cb 向上投射!!
    • 可能最简单的方法是向 Base.Contact 添加一个复制构造函数,然后您可以 var cb = new Base.Contact( c );并且 cb 将是 Base.Contact 类型。不过,这感觉有点像 hack。
    • @Crazy 是的,如果你真的只想要基础,你必须按照 Neal 的描述来更新它,从派生类复制值。
    【解决方案2】:

    你绝对不能通过强制转换将一个类变成它的基类之一。根据您使用的序列化类型,您可以告诉序列化程序假设该类是基类型,这会将序列化限制为基类型的成员。

    【讨论】:

      【解决方案3】:

      我阅读了答案,但仍然不明白问题所在! 上面的代码有什么问题?


      此外,从 cmets 中,我了解到您只需要序列化基类。我认为没有问题,首先。看例子-

      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
      

      【讨论】:

        【解决方案4】:

        将复制构造函数添加到基类中(例如public Contact(Contact other)),然后您可以这样做:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-09
          相关资源
          最近更新 更多