Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Interface member's attributes

Interface member's attributes

Scheduled Pinned Locked Moved C#
ooptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Candyman
    wrote on last edited by
    #1

    Hello I was just wondering how do attributes in interface's members work. I mean, if I put an attribute in a property, for example: [BindableAttribute()] int PropertyName { get; set;} Will it propagate the attribute through inheritance without having to type again the attribute when implementing the interface code? thanks in advance.

    J J 3 Replies Last reply
    0
    • M Mr Candyman

      Hello I was just wondering how do attributes in interface's members work. I mean, if I put an attribute in a property, for example: [BindableAttribute()] int PropertyName { get; set;} Will it propagate the attribute through inheritance without having to type again the attribute when implementing the interface code? thanks in advance.

      J Offline
      J Offline
      John Gathogo
      wrote on last edited by
      #2

      You will also need to implement the properties when implementing the interfaces like:

          protected string m\_PropertyName;
      
          public string PropertyName
          {
              get
              {
                  return m\_PropertyName;
              }
              internal set
              {
                  m\_PropertyName = value;
              }
          }
      

      So, it is expected that you will implement the get and set in derived classes

      M 1 Reply Last reply
      0
      • J John Gathogo

        You will also need to implement the properties when implementing the interfaces like:

            protected string m\_PropertyName;
        
            public string PropertyName
            {
                get
                {
                    return m\_PropertyName;
                }
                internal set
                {
                    m\_PropertyName = value;
                }
            }
        

        So, it is expected that you will implement the get and set in derived classes

        M Offline
        M Offline
        Mr Candyman
        wrote on last edited by
        #3

        Thanks but I do know that I need to implement the properties accessors code, what I wanted to learn is whether I have to explicitly implement the attribute too(the BindableAttribute in this case) each time again even if I put it in the interface.

        J 1 Reply Last reply
        0
        • M Mr Candyman

          Thanks but I do know that I need to implement the properties accessors code, what I wanted to learn is whether I have to explicitly implement the attribute too(the BindableAttribute in this case) each time again even if I put it in the interface.

          J Offline
          J Offline
          John Gathogo
          wrote on last edited by
          #4

          Sorry for the misunderstanding Mr. Candyman. Am not exactly sure whether you have to still implement the attribute too.

          1 Reply Last reply
          0
          • M Mr Candyman

            Hello I was just wondering how do attributes in interface's members work. I mean, if I put an attribute in a property, for example: [BindableAttribute()] int PropertyName { get; set;} Will it propagate the attribute through inheritance without having to type again the attribute when implementing the interface code? thanks in advance.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Have you tried it? It would seem this is very easy to confirm with a 5 minute test-app.

            1 Reply Last reply
            0
            • M Mr Candyman

              Hello I was just wondering how do attributes in interface's members work. I mean, if I put an attribute in a property, for example: [BindableAttribute()] int PropertyName { get; set;} Will it propagate the attribute through inheritance without having to type again the attribute when implementing the interface code? thanks in advance.

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              Here you go, A simple test app.

              class Program
              {
              static void Main(string[] args)
              {
              ExecTest(typeof(ITest));
              ExecTest(typeof(Test));
              Console.ReadLine();
              }

              static void ExecTest(Type type)
              {
                  PropertyInfo prop = type.GetProperty("PropertyName");
                  if (prop == null)
                      throw new InvalidOperationException("invalid type - must have PropertyName property");
                  object\[\] attrs = prop.GetCustomAttributes(typeof(DefaultSettingValueAttribute), true);
                  Console.WriteLine("Number of DefaultSettingValueAttribute's on '{1}': {0}", attrs.Length,type.Name);
              }
              
              
              public class Test : ITest
              {
                  #region ITest Members
              
                  public string PropertyName
                  {
                      get
                      {
                          throw new NotImplementedException();
                      }
                      set
                      {
                          throw new NotImplementedException();
                      }
                  }
              
                  #endregion
              }
              
              public interface ITest
              {
                  \[DefaultSettingValue("test")\]
                  string PropertyName { get; set; }
              }
              

              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups