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. Attributes giving problem

Attributes giving problem

Scheduled Pinned Locked Moved C#
csharplinqhelp
6 Posts 5 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.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    In the following code snippet myMath class has five Attributes, the problem which I am facing is that on the console I don't get the Order of the Attributes , right. The attribute with bugID = 1 shows on the top, and strangely it is followed by the fifth attribute bugID = 5, and then 2,3,4. When I remove the fifth Attribute this is the order of the Attributes 2,1,3,4. When there are three attributes the behavior is normal as in 1,2,3. When there are two attributes again the behavior changes to 2,1. Can Anyone pl. figure out the problem for me

    using System.Collections.Generic;
    using System;
    using System.Linq;
    using System.Text;
    using System.Reflection;

    namespace UsingAttributes
    {
    [AttributeUsage(AttributeTargets.All,AllowMultiple = true )]
    class BugFixAttribute : Attribute
    {
    private int bugID;
    private string coder;
    private string date;
    private string comment;
    public BugFixAttribute(int bugID, string coder, string date)
    {
    this.bugID = bugID;
    this.coder = coder;
    this.date = date;
    }

        public string Comment
        {
            get
            {
                return comment;
            }
            set
            {
                comment = value;
            }
        }
        public int BugID
        {
            get
            {
                return bugID;
            }
        }
    
        public string Coder
        {
            get
            {
                return coder;
            }
        }
    
        public string Date
        {
            get
            {
                return date;
            }
        }
    }
    \[BugFix(001, "sh", "08/11/2007", Comment = "one")\]
    \[BugFix(002, "sh", "08/11/2007", Comment = "two")\]
    \[BugFix(003, "sh", "08/11/2007", Comment = "three")\]
    \[BugFix(004, "sh", "08/11/2007", Comment = "four")\]
    \[BugFix(005, "sh", "08/11/2007", Comment = "five")\]
    public class myMath
    {
        public double DoFunc1(double param1)
        {
            return param1 + DoFunc2(param1);
        }
    
        public double DoFunc2(double param1)
        {
            return param1 / 3;
        }
    }
    class Program
    {
       
        
        static void Main(string\[\] args)
        {
            myMath mm = new myMath();
            Console.WriteLine("Result for DoFunc(7) = {0}", mm.DoFunc1(7));
    
            MemberInfo   inf = typeof(myMath);
    
    J P P N 4 Replies Last reply
    0
    • H humayunlalzad

      In the following code snippet myMath class has five Attributes, the problem which I am facing is that on the console I don't get the Order of the Attributes , right. The attribute with bugID = 1 shows on the top, and strangely it is followed by the fifth attribute bugID = 5, and then 2,3,4. When I remove the fifth Attribute this is the order of the Attributes 2,1,3,4. When there are three attributes the behavior is normal as in 1,2,3. When there are two attributes again the behavior changes to 2,1. Can Anyone pl. figure out the problem for me

      using System.Collections.Generic;
      using System;
      using System.Linq;
      using System.Text;
      using System.Reflection;

      namespace UsingAttributes
      {
      [AttributeUsage(AttributeTargets.All,AllowMultiple = true )]
      class BugFixAttribute : Attribute
      {
      private int bugID;
      private string coder;
      private string date;
      private string comment;
      public BugFixAttribute(int bugID, string coder, string date)
      {
      this.bugID = bugID;
      this.coder = coder;
      this.date = date;
      }

          public string Comment
          {
              get
              {
                  return comment;
              }
              set
              {
                  comment = value;
              }
          }
          public int BugID
          {
              get
              {
                  return bugID;
              }
          }
      
          public string Coder
          {
              get
              {
                  return coder;
              }
          }
      
          public string Date
          {
              get
              {
                  return date;
              }
          }
      }
      \[BugFix(001, "sh", "08/11/2007", Comment = "one")\]
      \[BugFix(002, "sh", "08/11/2007", Comment = "two")\]
      \[BugFix(003, "sh", "08/11/2007", Comment = "three")\]
      \[BugFix(004, "sh", "08/11/2007", Comment = "four")\]
      \[BugFix(005, "sh", "08/11/2007", Comment = "five")\]
      public class myMath
      {
          public double DoFunc1(double param1)
          {
              return param1 + DoFunc2(param1);
          }
      
          public double DoFunc2(double param1)
          {
              return param1 / 3;
          }
      }
      class Program
      {
         
          
          static void Main(string\[\] args)
          {
              myMath mm = new myMath();
              Console.WriteLine("Result for DoFunc(7) = {0}", mm.DoFunc1(7));
      
              MemberInfo   inf = typeof(myMath);
      
      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      AFAIK the call to GetCustomAttributes does not do so in any recognisable order. I have faced a similar issue in the past where the order of arributes was important so I created a custom static method to get my attributes in an ordered fashion. In your case this could well use the BugID property to order on.

      1 Reply Last reply
      0
      • H humayunlalzad

        In the following code snippet myMath class has five Attributes, the problem which I am facing is that on the console I don't get the Order of the Attributes , right. The attribute with bugID = 1 shows on the top, and strangely it is followed by the fifth attribute bugID = 5, and then 2,3,4. When I remove the fifth Attribute this is the order of the Attributes 2,1,3,4. When there are three attributes the behavior is normal as in 1,2,3. When there are two attributes again the behavior changes to 2,1. Can Anyone pl. figure out the problem for me

        using System.Collections.Generic;
        using System;
        using System.Linq;
        using System.Text;
        using System.Reflection;

        namespace UsingAttributes
        {
        [AttributeUsage(AttributeTargets.All,AllowMultiple = true )]
        class BugFixAttribute : Attribute
        {
        private int bugID;
        private string coder;
        private string date;
        private string comment;
        public BugFixAttribute(int bugID, string coder, string date)
        {
        this.bugID = bugID;
        this.coder = coder;
        this.date = date;
        }

            public string Comment
            {
                get
                {
                    return comment;
                }
                set
                {
                    comment = value;
                }
            }
            public int BugID
            {
                get
                {
                    return bugID;
                }
            }
        
            public string Coder
            {
                get
                {
                    return coder;
                }
            }
        
            public string Date
            {
                get
                {
                    return date;
                }
            }
        }
        \[BugFix(001, "sh", "08/11/2007", Comment = "one")\]
        \[BugFix(002, "sh", "08/11/2007", Comment = "two")\]
        \[BugFix(003, "sh", "08/11/2007", Comment = "three")\]
        \[BugFix(004, "sh", "08/11/2007", Comment = "four")\]
        \[BugFix(005, "sh", "08/11/2007", Comment = "five")\]
        public class myMath
        {
            public double DoFunc1(double param1)
            {
                return param1 + DoFunc2(param1);
            }
        
            public double DoFunc2(double param1)
            {
                return param1 / 3;
            }
        }
        class Program
        {
           
            
            static void Main(string\[\] args)
            {
                myMath mm = new myMath();
                Console.WriteLine("Result for DoFunc(7) = {0}", mm.DoFunc1(7));
        
                MemberInfo   inf = typeof(myMath);
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Jamies entirely right in what he said about retrieving the attributes by order. What you could do is implement a delegate to sort them into order in your reading code.

        Deja View - the feeling that you've seen this post before.

        1 Reply Last reply
        0
        • H humayunlalzad

          In the following code snippet myMath class has five Attributes, the problem which I am facing is that on the console I don't get the Order of the Attributes , right. The attribute with bugID = 1 shows on the top, and strangely it is followed by the fifth attribute bugID = 5, and then 2,3,4. When I remove the fifth Attribute this is the order of the Attributes 2,1,3,4. When there are three attributes the behavior is normal as in 1,2,3. When there are two attributes again the behavior changes to 2,1. Can Anyone pl. figure out the problem for me

          using System.Collections.Generic;
          using System;
          using System.Linq;
          using System.Text;
          using System.Reflection;

          namespace UsingAttributes
          {
          [AttributeUsage(AttributeTargets.All,AllowMultiple = true )]
          class BugFixAttribute : Attribute
          {
          private int bugID;
          private string coder;
          private string date;
          private string comment;
          public BugFixAttribute(int bugID, string coder, string date)
          {
          this.bugID = bugID;
          this.coder = coder;
          this.date = date;
          }

              public string Comment
              {
                  get
                  {
                      return comment;
                  }
                  set
                  {
                      comment = value;
                  }
              }
              public int BugID
              {
                  get
                  {
                      return bugID;
                  }
              }
          
              public string Coder
              {
                  get
                  {
                      return coder;
                  }
              }
          
              public string Date
              {
                  get
                  {
                      return date;
                  }
              }
          }
          \[BugFix(001, "sh", "08/11/2007", Comment = "one")\]
          \[BugFix(002, "sh", "08/11/2007", Comment = "two")\]
          \[BugFix(003, "sh", "08/11/2007", Comment = "three")\]
          \[BugFix(004, "sh", "08/11/2007", Comment = "four")\]
          \[BugFix(005, "sh", "08/11/2007", Comment = "five")\]
          public class myMath
          {
              public double DoFunc1(double param1)
              {
                  return param1 + DoFunc2(param1);
              }
          
              public double DoFunc2(double param1)
              {
                  return param1 / 3;
              }
          }
          class Program
          {
             
              
              static void Main(string\[\] args)
              {
                  myMath mm = new myMath();
                  Console.WriteLine("Result for DoFunc(7) = {0}", mm.DoFunc1(7));
          
                  MemberInfo   inf = typeof(myMath);
          
          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Or make an attribute collection?

          [BugFixAttributeCollection(
          new BugFix(001, "sh", "08/11/2007", Comment = "one") ,
          new BugFix(002, "sh", "08/11/2007", Comment = "two") ,
          new BugFix(003, "sh", "08/11/2007", Comment = "three") ,
          new BugFix(004, "sh", "08/11/2007", Comment = "four") ,
          new BugFix(005, "sh", "08/11/2007", Comment = "five")
          )]

          I'd better try it to see whether or not it works... P.S. Use an ISO 8601 compliant date format. -- modified at 21:50 Thursday 8th November, 2007 Well it doesn't work as shown above, but I'm getting close...

          P 1 Reply Last reply
          0
          • P PIEBALDconsult

            Or make an attribute collection?

            [BugFixAttributeCollection(
            new BugFix(001, "sh", "08/11/2007", Comment = "one") ,
            new BugFix(002, "sh", "08/11/2007", Comment = "two") ,
            new BugFix(003, "sh", "08/11/2007", Comment = "three") ,
            new BugFix(004, "sh", "08/11/2007", Comment = "four") ,
            new BugFix(005, "sh", "08/11/2007", Comment = "five")
            )]

            I'd better try it to see whether or not it works... P.S. Use an ISO 8601 compliant date format. -- modified at 21:50 Thursday 8th November, 2007 Well it doesn't work as shown above, but I'm getting close...

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I don't suppose you'd want something like this...

            [GroupOfAttribute(
            "<OneAttribute Num=\"001\" Text=\"sh\" When=\"2007-11-08\" />"
            ,
            "<OneAttribute Num=\"002\" Text=\"sh\" When=\"2007-11-08\" />"
            ,
            "<OneAttribute Num=\"003\" Text=\"sh\" When=\"2007-11-08\" />"
            )]

            Ugly ugly ugly... error prone and not very typesafe... but it works! :-D

            1 Reply Last reply
            0
            • H humayunlalzad

              In the following code snippet myMath class has five Attributes, the problem which I am facing is that on the console I don't get the Order of the Attributes , right. The attribute with bugID = 1 shows on the top, and strangely it is followed by the fifth attribute bugID = 5, and then 2,3,4. When I remove the fifth Attribute this is the order of the Attributes 2,1,3,4. When there are three attributes the behavior is normal as in 1,2,3. When there are two attributes again the behavior changes to 2,1. Can Anyone pl. figure out the problem for me

              using System.Collections.Generic;
              using System;
              using System.Linq;
              using System.Text;
              using System.Reflection;

              namespace UsingAttributes
              {
              [AttributeUsage(AttributeTargets.All,AllowMultiple = true )]
              class BugFixAttribute : Attribute
              {
              private int bugID;
              private string coder;
              private string date;
              private string comment;
              public BugFixAttribute(int bugID, string coder, string date)
              {
              this.bugID = bugID;
              this.coder = coder;
              this.date = date;
              }

                  public string Comment
                  {
                      get
                      {
                          return comment;
                      }
                      set
                      {
                          comment = value;
                      }
                  }
                  public int BugID
                  {
                      get
                      {
                          return bugID;
                      }
                  }
              
                  public string Coder
                  {
                      get
                      {
                          return coder;
                      }
                  }
              
                  public string Date
                  {
                      get
                      {
                          return date;
                      }
                  }
              }
              \[BugFix(001, "sh", "08/11/2007", Comment = "one")\]
              \[BugFix(002, "sh", "08/11/2007", Comment = "two")\]
              \[BugFix(003, "sh", "08/11/2007", Comment = "three")\]
              \[BugFix(004, "sh", "08/11/2007", Comment = "four")\]
              \[BugFix(005, "sh", "08/11/2007", Comment = "five")\]
              public class myMath
              {
                  public double DoFunc1(double param1)
                  {
                      return param1 + DoFunc2(param1);
                  }
              
                  public double DoFunc2(double param1)
                  {
                      return param1 / 3;
                  }
              }
              class Program
              {
                 
                  
                  static void Main(string\[\] args)
                  {
                      myMath mm = new myMath();
                      Console.WriteLine("Result for DoFunc(7) = {0}", mm.DoFunc1(7));
              
                      MemberInfo   inf = typeof(myMath);
              
              N Offline
              N Offline
              Nissim Salomon
              wrote on last edited by
              #6

              Hi I saw from your example that you are using framework 3.0 or above Try using the follwoing code

              List<BugFixAttribute> lst = new List<BugFixAttribute>((IEnumerable<BugFixAttribute> )inf.GetCustomAttributes(typeof(BugFixAttribute), false));

              lst.Sort((a, b) => a.BugID < b.BugID ? -1 : 1);

              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