How to Extend Partial Class?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
Is it possible to extend a partial class?

Thanks
4 years ago
Hello havana7,

Please refer this blog for extending partial class in c# - https://dotnetcodr.com/2016/02/25/extending-class-definitions-with-partial-classes-in-c-net-2/
4 years ago
Certainly.  It's typically recommended to save the extended file in its own directory, for example here is how I added length, width, and height to the Shipment domain:

Nop.Core\Domain\Shipping\Extensions\ShipmentExtended.cs


using System;

namespace Nop.Core.Domain.Shipping
{
    /// <summary>
    /// Represents a shipment extension
    /// </summary>
    public partial class Shipment
    {
        /// <summary>
        /// Package Length
        /// </summary>
        public int? Length { get; set; }
        /// <summary>
        /// Package Width
        /// </summary>
        public int? Width { get; set; }
        /// <summary>
        /// Package Height
        /// </summary>
        public int? Height { get; set; }
    }
}


Same theory applies to partial Services, Controllers, Models, etc.
4 years ago
Sorry, I meant to extend into my plugin.
4 years ago
try using the same namespace as the original partial class, but I don't think you can have partial classes in different assemblies.  Depending on what you're trying to do, there is the ModelPreparedEvent as well as many other events, or maybe even action filters.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.