public void CanGenerateCompositeKey() { string expectedClassName = "ClassWithCompositeKey"; Type type = Assembly.GetExecutingAssembly().GetType("Debugging.Tests." + expectedClassName); PropertyInfo property = type.GetProperty(expectedClassName + "CompositeKey"); object[] propertyAttributes = property.GetCustomAttributes(typeof(CompositeKeyAttribute), false); Assert.IsTrue(propertyAttributes.Length == 1, "Did not generate CompositeKeyAttribute."); CompositeKeyAttribute attribute = propertyAttributes[0] as CompositeKeyAttribute; Assert.IsNotNull(attribute, "Did not generate CompositeKeyAttribute."); Assert.IsTrue(property.GetGetMethod().ReturnType.Name == expectedClassName + "CompositeKey"); Type ckType = Assembly.GetExecutingAssembly().GetType("Debugging.Tests." + expectedClassName + "CompositeKey"); Assert.IsNotNull(ckType, "Did not generate Helper class for composite key."); PropertyInfo keyProperty1 = ckType.GetProperty("Key1"); object[] keyAttributes = keyProperty1.GetCustomAttributes(typeof(KeyPropertyAttribute), false); Assert.IsTrue(keyAttributes.Length == 1, "Did not generate KeyPropertyAttribute."); KeyPropertyAttribute keyAttribute = keyAttributes[0] as KeyPropertyAttribute; Assert.IsNotNull(keyAttribute, "Did not generate KeyPropertyAttribute."); ClassWithCompositeKeyCompositeKey ck = new ClassWithCompositeKeyCompositeKey(); ck.Key1 = 1; ck.Key2 = 2; Assert.AreEqual(ck.ToString(), "1:2"); Assert.IsFalse(ck.Equals(null)); Assert.IsFalse(ck.Equals(new object())); Assert.IsFalse(ck.Equals(new ClassWithCompositeKeyCompositeKey())); ClassWithCompositeKeyCompositeKey ck2 = new ClassWithCompositeKeyCompositeKey(); ck2.Key1 = 1; ck2.Key2 = 2; Assert.IsTrue(ck.Equals(ck2)); }
public override bool Equals(object obj) { if ((obj == this)) { return(true); } if (((obj == null) || (obj.GetType() != this.GetType()))) { return(false); } ClassWithCompositeKeyCompositeKey test = ((ClassWithCompositeKeyCompositeKey)(obj)); return(((_key1 == test._key1) || ((_key1 != null) && _key1.Equals(test._key1))) && ((_key2 == test._key2) || ((_key2 != null) && _key2.Equals(test._key2)))); }