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