Dilemma over the usage of AttributesToIgnore.Add()

Apr 18, 2012 at 1:57 PM
Edited Apr 18, 2012 at 1:57 PM

Hi Greg,

In the sample mentioned below:

public class firstClass{

public firstClass()

{

this.secondClassCollection=new Hashset<secondClass>();

}

public int statusId{get; set;}

public string Name{get; set;}

public ICollection <secondClass> secondClassCollection {get; set;}

}

public class secondClass{

public int Id{get; set;}

}

 I am able to ignore comparing the secondClassCollection property while comparing the objects of the firstClass by using ElementsToIgnore.Add("secondClass") or by using AttributesToIgnore.Add(typeof(secondClass)). Could you please tell me which of these is the correct approach for ignoring comparing the secondClassCollection property while comparing the objects of the firstClass.

Coordinator
Apr 18, 2012 at 2:17 PM

I would create a custom attribute and decorate it on the property.  Then put it in the Attributes to ignore.

Apr 19, 2012 at 9:17 AM
Edited Apr 19, 2012 at 10:17 AM

Hi Greg,

I created a custom attribute just like you said as shown below:


namespace SampleApplication
{
    [System.AttributeUsage(AttributeTargets.Property)]
    public class Ignore: System.Attribute
    {
        private string m_propertyName;
        public Ignore(string propertyName)
        {
            this.m_propertyName = propertyName;
        }
    }

I used this custom attribute on all the properties that I want to Ignore, and then I put it in the attributesToIgnore(typeof(IgnoreAttribute)), will all those properties be ignored during the comparison process. I am still not clear about how this attributesToIgnore() works. Could you please elaborate on this.

 


Apr 26 at 1:33 AM
@swainjena

Not sure if you figured this out yet, but if you take a look at Greg's Unit Tests you can figure out how to use the attributes. Specifically:

Attribute class setup

Decoration of a Property

Leveraging its usage in a comparison (IgnorePropertyAttribute method)



@Greg

Great code, very useful; thanks for sharing with the community.

Joe