コード例 #1
0
 public static bool IsMatchFor(
     this PropertyOrField master,
     PropertyOrField other
     )
 {
     return(master.IsMatchFor(other, false));
 }
コード例 #2
0
        public void PropertyOrField_Get_Set_4()
        {
            CanaryClass canary = new CanaryClass
            {
                Int32Property = 123
            };
            Type         type = canary.GetType();
            PropertyInfo info = type.GetProperty("Int32Property");

            Assert.IsNotNull(info);
            PropertyOrField property = new PropertyOrField(info);

            Assert.AreSame(info, property.PropertyInfo);
            Assert.IsNull(property.FieldInfo);
            Assert.AreEqual("Int32Property", property.Name);
            Assert.IsTrue(property.MemberType == typeof(int));
            Assert.IsTrue(property.IsProperty);
            Assert.IsFalse(property.IsIndexed);
            Assert.IsFalse(property.ReadOnly);

            Assert.AreEqual(123, property.GetValue(canary));

            property.SetValue(canary, 321);
            Assert.AreEqual(321, canary.Int32Property);

            Assert.IsTrue(property.HaveAttribute <DisplayNameAttribute>(false));
            DisplayNameAttribute attribute = property.GetCustomAttribute <DisplayNameAttribute>(false);

            Assert.IsNotNull(attribute);
            Assert.AreEqual("CanaryProperty", attribute.DisplayName);

            Assert.AreEqual("Int32Property", property.ToString());
        }
コード例 #3
0
 public static bool IsAssignmentMatchFor(
     this PropertyOrField master,
     PropertyOrField other
     )
 {
     return(master.IsAssignmentMatchFor(other, true));
 }
コード例 #4
0
            private Expression GetAreEqualPropertyValuesExpression(
                PropertyOrField property,
                Expression lhsParameterExpression,
                Expression rhsParameterExpression,
                IEnumerable <Type> typesCurrentlyUnderConstruction)
            {
                var lhsPropertyValueExpression =
                    BoxIfNecessary(Expression.PropertyOrField(lhsParameterExpression, property.Name));
                var rhsPropertyValueExpression =
                    BoxIfNecessary(Expression.PropertyOrField(rhsParameterExpression, property.Name));

                if (property.PropertyOrFieldType.IsSealed &&
                    !CanCreateEqualsCycle(property.PropertyOrFieldType, typesCurrentlyUnderConstruction))
                {
                    var equalsFunc = GetOptimizedEqualsFunc(property.PropertyOrFieldType, Type.EmptyTypes);
                    return
                        (Expression.Invoke(
                             Expression.Constant(equalsFunc),
                             lhsPropertyValueExpression,
                             rhsPropertyValueExpression));
                }

                var thisClosure  = Expression.Constant(this);
                var equalsMethod = typeof(IEqualityComparer).GetMethod("Equals");

                return
                    (Expression.Call(
                         thisClosure,
                         equalsMethod,
                         lhsPropertyValueExpression,
                         rhsPropertyValueExpression));
            }
コード例 #5
0
        public void PropertyOrField_Get_Set_3()
        {
            CanaryClass canary = new CanaryClass
            {
                StringField = "Text"
            };
            Type      type = canary.GetType();
            FieldInfo info = type.GetField("StringField");

            Assert.IsNotNull(info);
            PropertyOrField field = new PropertyOrField(info);

            Assert.AreSame(info, field.FieldInfo);
            Assert.IsNull(field.PropertyInfo);
            Assert.AreEqual("StringField", field.Name);
            Assert.IsTrue(field.MemberType == typeof(string));
            Assert.IsFalse(field.IsProperty);
            Assert.IsFalse(field.IsIndexed);
            Assert.IsFalse(field.ReadOnly);

            Assert.AreEqual("Text", field.GetValue(canary));

            field.SetValue(canary, "Text2");
            Assert.AreEqual("Text2", canary.StringField);

            Assert.IsFalse(field.HaveAttribute <DisplayNameAttribute>(false));

            Assert.AreEqual("StringField", field.ToString());
        }
コード例 #6
0
        public void PropertyOrField_Get_Set_1()
        {
            CanaryClass canary = new CanaryClass
            {
                Int32Field = 123
            };
            Type      type = canary.GetType();
            FieldInfo info = type.GetField("Int32Field");

            Assert.IsNotNull(info);
            PropertyOrField field = new PropertyOrField(info);

            Assert.AreSame(info, field.FieldInfo);
            Assert.IsNull(field.PropertyInfo);
            Assert.AreEqual("Int32Field", field.Name);
            Assert.IsTrue(field.MemberType == typeof(int));
            Assert.IsFalse(field.IsProperty);
            Assert.IsFalse(field.IsIndexed);
            Assert.IsFalse(field.ReadOnly);

            Assert.AreEqual(123, field.GetValue(canary));

            field.SetValue(canary, 321);
            Assert.AreEqual(321, canary.Int32Field);

            Assert.IsFalse(field.HaveAttribute <DisplayNameAttribute>(false));

            Assert.AreEqual("Int32Field", field.ToString());
        }
コード例 #7
0
        public void PropertyOrField_Get_Set_2()
        {
            CanaryClass canary = new CanaryClass
            {
                BooleanField = true
            };
            Type      type = canary.GetType();
            FieldInfo info = type.GetField("BooleanField");

            Assert.IsNotNull(info);
            PropertyOrField field = new PropertyOrField(info);

            Assert.AreSame(info, field.FieldInfo);
            Assert.IsNull(field.PropertyInfo);
            Assert.AreEqual("BooleanField", field.Name);
            Assert.IsTrue(field.MemberType == typeof(bool));
            Assert.IsFalse(field.IsProperty);
            Assert.IsFalse(field.IsIndexed);
            Assert.IsFalse(field.ReadOnly);

            Assert.AreEqual(true, field.GetValue(canary));

            field.SetValue(canary, false);
            Assert.AreEqual(false, canary.BooleanField);

            Assert.IsFalse(field.HaveAttribute <DisplayNameAttribute>(false));

            Assert.AreEqual("BooleanField", field.ToString());
        }
コード例 #8
0
                public override void SetRandomValue(
                    PropertyOrField propInfo,
                    ref object target)
                {
                    var value = GetRandomInt(100, 1024);

                    propInfo.SetValue(target, value);
                }
コード例 #9
0
 private static bool AccessMatches(
     this PropertyOrField master,
     PropertyOrField other
     )
 {
     return(master.CanRead == other.CanRead &&
            master.CanWrite == other.CanWrite);
 }
コード例 #10
0
        public void PropertyOrField_Construction_2()
        {
            Type       type   = typeof(CanaryClass);
            MemberInfo member = type.GetMethod("MethodOne");

            Assert.IsNotNull(member);
            PropertyOrField property = new PropertyOrField(member);

            Assert.IsNotNull(property);
        }
コード例 #11
0
        public void PropertyOrField_Construction_1()
        {
            Type       type   = typeof(CanaryClass);
            MemberInfo member = type.GetProperty("Int32Property");

            Assert.IsNotNull(member);
            PropertyOrField property = new PropertyOrField(member);

            Assert.IsNotNull(property);
        }
コード例 #12
0
 /// <summary>
 /// Tests if this PropertyOrField is a match for another
 /// </summary>
 /// <param name="master"></param>
 /// <param name="other"></param>
 /// <param name="mustMatchMemberType"></param>
 /// <returns></returns>
 public static bool IsMatchFor(
     this PropertyOrField master,
     PropertyOrField other,
     bool mustMatchMemberType
     )
 {
     return(master.Name == other.Name &&
            master.Type == other.Type &&
            master.AccessMatches(other) &&
            (!mustMatchMemberType || master.MemberType == other.MemberType));
 }
コード例 #13
0
        public static bool IsAssignmentMatchFor(
            this PropertyOrField master,
            PropertyOrField other,
            bool mustMatchMemberType
            )
        {
#pragma warning disable S1067
            return(master.Name == other.Name &&
                   other.Type.IsAssignableOrUpcastableTo(master.Type) &&
                   master.AccessMatches(other) &&
                   (!mustMatchMemberType || master.MemberType == other.MemberType));

#pragma warning restore S1067
        }
コード例 #14
0
        public void PropertyOrField_CompareTo_1()
        {
            Type      type = typeof(CanaryClass);
            FieldInfo info = type.GetField("Int32Field");

            Assert.IsNotNull(info);
            PropertyOrField first  = new PropertyOrField(info);
            PropertyOrField second = new PropertyOrField(info);

            Assert.IsTrue(((IComparable <PropertyOrField>)first).CompareTo(second) == 0);

            info = type.GetField("BooleanField");
            Assert.IsNotNull(info);
            second = new PropertyOrField(info);
            Assert.IsTrue(((IComparable <PropertyOrField>)first).CompareTo(second) != 0);
        }
コード例 #15
0
 /// <summary>
 /// Actually invoked when attempting to set a random value on the
 /// named property
 /// </summary>
 /// <param name="propInfo"></param>
 /// <param name="target"></param>
 public abstract void SetRandomValue(
     PropertyOrField propInfo,
     ref object target);
コード例 #16
0
 /// <inheritdoc />
 public override void SetRandomValue(PropertyOrField propInfo,
                                     ref object target)
 {
     propInfo.SetValue(target, RandomValueGen.GetRandomInt(1));
 }
コード例 #17
0
 /// <inheritdoc />
 public override void SetRandomValue(PropertyOrField propInfo, ref object target)
 {
     /* intentionally left blank */
 }
コード例 #18
0
 /// <summary>
 /// Prints out a pretty representation of this PropertyOrField
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 // ReSharper disable once UnusedMember.Global
 public static string PrettyPrint(this PropertyOrField item)
 {
     return($"{item.Name} ({item.Type}) [{item.PrintAccess()}]");
 }
コード例 #19
0
 public static string PrintAccess(this PropertyOrField item)
 {
     return(AccessNames
            .First(t => t.Item1 == item.CanRead && t.Item2 == item.CanWrite)
            .Item3);
 }
コード例 #20
0
 /// <inheritdoc />
 public override void SetRandomValue(PropertyOrField propInfo,
                                     ref object target)
 {
     propInfo.SetValue(target, _generator.NextObjectValue());
 }