Copy two identical object with different namespaces (recursive reflection)

后端 未结 7 1481
我寻月下人不归
我寻月下人不归 2021-01-06 19:22

I\'m working in c# with several workspaces that have one specific class which his always the same in each workspace. I would like to be able have a copy of this class to be

7条回答
  •  爱一瞬间的悲伤
    2021-01-06 20:00

    Two identical or similar objects from different namespaces ?

    You have this:

    namespace Cars
    {
      public class car {
        public string Name;
        public void Start() { ... }
      }
    } 
    
    
    namespace Planes
    {
      public class plane {
        public string Name;
    public void Fly() { ... }
      }
    } 
    

    Time to apply some class inheritance:

    namespace Vehicles
    {
      public class vehicle
      {
        public string Name;
      } // class
    } // namespace
    
    using Vehicles;
    namespace Cars
    {
      public class car: vehicle
      {
        public string Name;
        public void Start() { ... }
      }  // class
    } // namespace
    
    using Vehicles;
    namespace Planes
    {
      public class plane: vehicle
      {
        public void Fly() { ... }
      }
    } 
    

    And to copy, there is a copy method or constructor, but, I prefer a custom one:

    namespace Vehicles
    {
      public class vehicle {
        public string Name;
    
        public virtual CopyFrom (vehicle Source)
        {
          this.Name = Source.Name;
          // other fields
        }
      } // class
    
    } // namespace 
    

    Cheers.

提交回复
热议问题