简体   繁体   中英

Making auto generated partial class implement custom interface

Lately I've been working on a Silverlight project in which there is an auto generated class located in * File.Web.g.cs with a definition:

[DataContract(Namespace="http://schemas.datacontract.org/2004/07/Project.Web.Models")]
public sealed partial class SwitchDevice : Entity
{
        /// <summary>
        /// Gets or sets the 'ID' value.
        /// </summary>
        [DataMember()]
        [Editable(false, AllowInitialValue=true)]
        [Key()]
        [RoundtripOriginal()]
        public int ID
        {
            get
            {
                return this._id;
            }
            set
            {
                if ((this._id != value))
                {
                    this.OnIDChanging(value);
                    this.ValidateProperty("ID", value);
                    this._id = value;
                    this.RaisePropertyChanged("ID");
                    this.OnIDChanged();
                }
            }
        }
}

What I wanted to do is to make SwitchDevice implement the IDevice interface. Code is located in another file named IDevice.cs . I decided that I will extend the partial class like this:

namespace Project.Web.Models
{
  public interface IDevice
  {
      int ID
      {
          get;
          set;
      }
   }

  public partial class SwitchDevice : IDevice
  {
  }
}

For some reason VS2010 does not see the definition located in Web.G.CS file and generates error:

'Project.Web.Models.SwitchDevice' does not implement interface member 'Project.Web.Models.IDevice.ID' PATH\\Project\\Interfaces\\ISwitchDevice.cs

I've browsed and read a lot of web pages but I haven't really found a similar issue. Maybe I'm just doing it wrong, I'm not an expert.

Is it even possible to make this work? Thank you in advance for any tips and help!

what you try to do is fine and i do it all the times.

Make sure you have both partial class definition files under same namespace AND inside the same project. the interface you are implementing with that class will be probably in another assembly or you lose part of the advantages and will have to reference your dal anyway from the calling code and this is one thing you design could give you; isolation from DAL and coding against interfaces.

In all likelihood, the auto-generated class SwitchDevice (the sealed one) is not in the namespace Project.Web.Models like the class SwitchDevice that you wrote is.

For partial classes to work as you expect, all parts of the class definition should be in the same namespace. Otherwise, you just have two (unrelated) classes with the same name in different namespaces, and the partial modifier in both of them does not play any role.

If this is true, what you need to do is move your own class fragment into the same namespace as the auto-generated class, eg:

namespace Project.Web.Models
{
    public interface IDevice
    {
        int ID { get; set; }
    }
}

namespace Some.Namespace // copy the name from the auto-generated file
{
    public partial class SwitchDevice : Project.Web.Models.IDevice
    {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM