0

check that I have a customAttribute called ResponseAttributes. I have declared an interface and several concrete classes { Question 1, Question2, Question3 }

[AttributeUsage(AttributeTargets.Property)]
public class ResponseAttribute : Attribute { }
public interface IQuestion { }

public class Question1 : IQuestion
{
    [Response]
    public string Response { get; set; }
    public Question1() { Response = "2+1"; }
}

public class Question2 : IQuestion
{
    [Response]
    public decimal Response { get; set; }
    public Question2() { Response = 5; }
}

public class Question3 : IQuestion
{
    [Response]
    public string Response { get; set; }
    public Question3() { Response = "2+1"; }
}

Now, what I'm trying to do is how to verify a class which contains that attribute is equals to another?

I mean:

        List<IQuestion> questions = new List<IQuestion>()
        {
            new Question1(), new Question2()
        };

        Question3 question3 = new Question3();

        foreach (var question in questions)
        {
            // how to verify this condition:
            // if (customAttribute from question3 is equals to customAttribute fromquestion)
            // of course the question3 is equals to 1
        }

As you can, they are different types, that's the reason why I set as ResponseAttribute.

Gray
  • 7,050
  • 2
  • 29
  • 52
Darf Zon
  • 6,268
  • 20
  • 90
  • 149
  • 1
    Why do you need an attribute at all? If your interface included a Response property (i.e. decimal Response { get;set;} ), you could compare your variables through the common property provided by the interface – kristian Apr 06 '12 at 05:27
  • The problem here, is every questions has a response property, but each one is different datatype, so I was thinking that is good to have the customAttribute – Darf Zon Apr 06 '12 at 14:47
  • One of the benefits of using an interface IQuestion is that you can enforce the use of a standard comparable data type for some output. You may not want to change how Response works (keeping different datatypes) but you may consider adding an interface property `Answer` as a string that, upon implementation, outputs the Response as a string for comparison. Then your for-each becomes trivial, `if questionA.Answer == questionB.Answer...` – EtherDragon Oct 17 '13 at 22:00

2 Answers2

1

you could try using an interface, with Resposnse property (type object) if you can not, you could use a class-level attribute that tell you the "Response property" than, you can use reflection on that property

Example:


public class ResponseAttribute : Attribute { 
  public string PropertyName { get; set }
}

[ResponseAttribute ("CustomResponse")}
public class Question1 {
   public string CustomResponse;
}

via reflection
foreach(var question in questions) {
   var responseAttr = (ResponseAttribute) question.GetType().GetCustomAttributes(typeof(ResponseAttribute));

var questionResponse= question.GetType().GetProperty(responseAttr.PropertyName,question,null);
}

stefano m
  • 4,094
  • 5
  • 28
  • 27
0

try overriding equals method:

    public override bool Equals(object obj)
    {
        if (obj is IQuestion)
            return this.Response == ((IQuestion)obj).Response;
        else
            return base.Equals(obj);
    }

hope this helps

slawekwin
  • 6,270
  • 1
  • 44
  • 57