Example #1
0
 public void TestAccessStaticMemberViaMemberInfo()
 {
     RunWith((Type type) => {
         System.Reflection.MemberInfo memberInfo = type.Member("TotalPeopleCreated", FasterflectFlags.StaticAnyVisibility);
         int totalPeopleCreated = (int)memberInfo.Get() + 1;
         memberInfo.Set(totalPeopleCreated);
         VerifyProperties(type, new { totalPeopleCreated });
     });
 }
 public Maybe<object> ForMember(MemberInfo member, Func<object> instanceAccessor)
 {
     Func<object> propertyAccessor = () => member.Get(instanceAccessor());
     var fieldType = member.GetMemberType();
     foreach (var builder in Builders.Where(builder => builder.Key(fieldType)))
     {
         return builder.Value(member, propertyAccessor);
     }
     return new Nothing<object>();
 }
Example #3
0
        private void RegisterMember(Type type, Reflection.MemberInfo m, Type mType, Func<object, object> get, Action<object, object> set)
        {
            if (mType.IsSubclassOf(typeof(Delegate)))
                return;

            // struct that holds access method for property/field
            MemberInfo accessor = new MemberInfo();

            accessor.Type = mType;
            accessor.Get = get;
            accessor.Set = set;

            if(set!=null){ // writeable ?
                accessor.SerializeMethod = YamlSerializeMethod.Assign;
            } else {
                accessor.SerializeMethod = YamlSerializeMethod.Never;
                if ( mType.IsClass )
                    accessor.SerializeMethod = YamlSerializeMethod.Content;
            }

            var field = m as FieldInfo;
            var prop = m as PropertyInfo;

            var attr1 = m.GetAttribute<YamlSerializeAttribute>();
            if ( attr1 != null ) { // specified
                if ( set == null ) { // read only member
                    if ( attr1.SerializeMethod == YamlSerializeMethod.Assign ||
                         ( mType.IsValueType && accessor.SerializeMethod == YamlSerializeMethod.Content ) )
                        throw new ArgumentException("{0} {1} is not writeable by {2}."
                            .DoFormat(mType.FullName, m.Name, attr1.SerializeMethod.ToString()));
                }
                accessor.SerializeMethod = attr1.SerializeMethod;
            }
            else
            {
                //if there are no attributes, and the field/property is private, internal, or protected, set it to not serialize
                if (field != null)
                {
                    if (field.IsFamilyOrAssembly || field.IsPrivate)
                        accessor.SerializeMethod = YamlSerializeMethod.Never;
                }
                else if (prop != null)
                {
                    if (accessor.Get.Method.IsFamilyOrAssembly || accessor.Get.Method.IsPrivate)
                        accessor.SerializeMethod = YamlSerializeMethod.Never;
                }
            }

            if ( accessor.SerializeMethod == YamlSerializeMethod.Never )
                return; // no need to register
            if ( accessor.SerializeMethod == YamlSerializeMethod.Binary ) {
                if ( !mType.IsArray )
                    throw new InvalidOperationException("{0} {1} of {2} is not an array. Can not be serialized as binary."
                        .DoFormat(mType.FullName, m.Name, type.FullName));
                if ( !TypeUtils.IsPureValueType(mType.GetElementType()) )
                    throw new InvalidOperationException(
                        "{0} is not a pure ValueType. {1} {2} of {3} can not serialize as binary."
                        .DoFormat(mType.GetElementType(), mType.FullName, m.Name, type.FullName));
            }

            // ShouldSerialize
            //      YamlSerializeAttribute(Never) => false
            //      ShouldSerializeSomeProperty => call it
            //      DefaultValueAttribute(default) => compare to it
            //      otherwise => true
            var shouldSerialize = type.GetMethod("ShouldSerialize" + m.Name,
                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic,
                null, Type.EmptyTypes, new System.Reflection.ParameterModifier[0]);
            if ( shouldSerialize != null && shouldSerialize.ReturnType == typeof(bool) && accessor.ShouldSeriealize == null )
                accessor.ShouldSeriealize = obj => (bool)shouldSerialize.Invoke(obj, EmptyObjectArray);
            var attr2 = m.GetAttribute<DefaultValueAttribute>();
            if ( attr2 != null && accessor.ShouldSeriealize == null ) {
                var defaultValue = attr2.Value;
                if ( TypeUtils.IsNumeric(defaultValue) && defaultValue.GetType() != mType )
                    defaultValue = TypeUtils.CastToNumericType(defaultValue, mType);
                accessor.ShouldSeriealize = obj => !TypeUtils.AreEqual(defaultValue, accessor.Get(obj));
            }
            if ( accessor.ShouldSeriealize == null )
                accessor.ShouldSeriealize = obj => true;

            Accessors.Add(m.Name, accessor);
        }