Пример #1
0
    public override void InitStats()
    {
        base.InitStats();


        Getters.Add("Killstreak", delegate() { return(Killstreak); });

        Setters.Add("Killstreak", delegate(object val) { Killstreak = (int)val; });
    }
Пример #2
0
        private void CachePropertyGetter(string propertyName)
        {
            if (Getters.ContainsKey(propertyName))
            {
                return;
            }

            var propInfo = GetType().GetProperty(propertyName);

            Getters.Add(propertyName, obj => propInfo.GetValue(obj));
        }
Пример #3
0
    public override void InitStats()
    {
        base.InitStats();

        Getters.Add("Deaths", delegate() { return(Deaths); });
        Getters.Add("Frags", delegate() { return(Frags); });
        Getters.Add("Score", delegate() { return(Score); });

        Setters.Add("Deaths", delegate(object val) { Deaths = (int)val; });
        Setters.Add("Frags", delegate(object val) { Frags = (int)val; });
        Setters.Add("Score", delegate(object val) { Score = (int)val; });
    }
Пример #4
0
        private void Initialize(Type type)
        {
            Type underlying;

            if (type.IsNullable(out underlying) && !type.IsNullablePrimitive())
            {
                Type = underlying;
            }
            else
            {
                Type = type;
            }

            if (Type.IsPrimitive())
            {
                throw new InvalidOperationException("The primitive type cannot be cached.");
            }

            Properties = Type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.GetIndexParameters().Length == 0).ToList();

            var getters = Type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                          .Where(x => x.CanRead && x.GetIndexParameters().Length == 0 && x.GetGetMethod(false) != null);

            foreach (var get in getters)
            {
                var targetParamExp = Expression.Parameter(typeof(object), "target");
                var castExp        = Expression.Convert(targetParamExp, Type);
                var getMethod      = get.GetGetMethod();
                var getExp         = Expression.Call(castExp, getMethod);
                var retExp         = Expression.Convert(getExp, typeof(object));
                var getter         = (Func <object, object>)Expression.Lambda(retExp, targetParamExp).Compile();
                Getters.Add(new Tuple <PropertyInfo, Func <object, object> >(get, getter));
            }

            var setters = Type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                          .Where(x => x.CanWrite && x.GetIndexParameters().Length == 0 && x.GetSetMethod(false) != null);

            if (!Type.IsValueType())
            {
                foreach (var set in setters)
                {
                    var targetParamExp = Expression.Parameter(typeof(object), "target");
                    var valueParamExp  = Expression.Parameter(typeof(object), "val");
                    var setMethod      = set.GetSetMethod();
                    var castTargetExp  = Expression.Convert(targetParamExp, Type);
                    var testValueExp   = Expression.NotEqual(valueParamExp, Expression.Constant(null));
                    var castValueExp   = Expression.Convert(valueParamExp, set.PropertyType);

                    var setValueExp = Expression.Call(castTargetExp, setMethod, castValueExp);
                    var testExp     = Expression.IfThen(testValueExp, setValueExp);
                    var setter      = (Action <object, object>)Expression.Lambda(testExp, targetParamExp, valueParamExp).Compile();
                    Setters.Add(new Tuple <PropertyInfo, Action <object, object> >(set, setter));
                }
                Constructor = Type.GetConstructor(Type.EmptyTypes);

                if (Constructor != null)
                {
                    var newExp = Expression.New(Constructor);
                    Launcher = (Func <object>)Expression.Lambda(newExp).Compile();
                }
            }
            else
            {
                foreach (var set in setters)
                {
                    Action <object, object> setter = (target, val) => set.SetValue(target, val, null);
                    Setters.Add(new Tuple <PropertyInfo, Action <object, object> >(set, setter));
                }
                var newExp     = Expression.New(Type);
                var convertExp = Expression.Convert(newExp, typeof(object));
                Launcher = (Func <object>)Expression.Lambda(convertExp).Compile();
            }
            // TODO: abstract? inherit?
        }
 public void AddGetter(string getter) => Getters.Add(getter);
Пример #6
0
        private bool ExtractAccessors(MemberInfo member, string fieldName, ref Type dataType, ref bool isKey)
        {
            // Locals
            var getter = default(FieldGetterDelegate);
            var setter = default(FieldSetterDelegate);

            // Homogenize fields and properties
            switch (member.MemberType)
            {
            case System.Reflection.MemberTypes.Field:
                var field = ((FieldInfo)member);
                dataType = field.FieldType;
                getter   = field.GetValue;
                setter   = field.SetValue;
                break;

            case System.Reflection.MemberTypes.Property:
                var property = ((PropertyInfo)member);
                dataType = property.PropertyType;
                getter   = property.GetValue;
                setter   = property.SetValue;
                break;

            default:
                // Ignore other member types
                return(false);
            }

            // Inspect fields for key, specifiable/actual data type
            var bigtableKey = member.GetCustomAttribute <BigTableKeyAttribute>();
            var genDataType = dataType.IsGenericType ? dataType.GetGenericTypeDefinition() : null;
            var isBigField  = genDataType == typeof(BigTableField <>);
            var isBigKey    = genDataType == typeof(BigTableKey <>);

            isKey = bigtableKey != null || genDataType == typeof(BigTableKey <>);
            var isBigWrapper = isBigField || isBigKey;

            IsBigBoxed.Add(fieldName, isBigWrapper);

            // Store accessors
            if (isBigWrapper)
            {
                var subDataType = dataType.GenericTypeArguments[0];
                var access      = isKey ? (BigAccess)GetKeyAccess(subDataType) : GetFieldAccess(subDataType);
                IsSpecified.Add(fieldName, target => ((IBigTableField)getter(target)).IsSpecified);
                Getters.Add(fieldName, target => access.ValueGetter(getter(target)));
                var useDataType = dataType;
                Setters.Add(fieldName, (target, value) =>
                {
                    var newValue = Activator.CreateInstance(useDataType);

                    access.ValueSetter(newValue, value);
                    setter(target, newValue);
                });

                dataType = subDataType;
            }
            else
            {
                IsSpecified.Add(fieldName, target => true);
                Getters.Add(fieldName, getter);
                Setters.Add(fieldName, setter);
            }

            // Indicate is property of field
            return(true);
        }