Пример #1
0
        public FieldInfo(TypeInfo typeInfo, System.Reflection.FieldInfo info, Type overrideType = null)
            : base(typeInfo, info, null, typeInfo.TypeCache.GetTypeInfo(overrideType ?? info.FieldType))
        {
            this.callGet = new Lazy <Func <object, object> >(() =>
            {
                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression getField             = Expression.Field(castTarget, info);
                Expression castField            = Expression.Convert(getField, typeof(object));

                return(ReflectionUtility.CompileLambda <Func <object, object> >(castField, targetParam));
            });

            this.callSet = new Lazy <Action <object, object> >(() =>
            {
                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                ParameterExpression valueParam  = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression castValue            = Expression.Convert(valueParam, info.FieldType);
                Expression accessField          = Expression.Field(castTarget, info);
                Expression setField             = Expression.Assign(accessField, castValue);

                return(ReflectionUtility.CompileLambda <Action <object, object> >(setField, targetParam, valueParam));
            });
        }
Пример #2
0
        private static void TryInjectValueInteractive(MemberInfo member, NukeBuild build)
        {
            if (!member.HasCustomAttribute <ParameterAttribute>())
            {
                return;
            }

            if (member is PropertyInfo property && !property.CanWrite)
            {
                return;
            }

            var memberType        = member.GetMemberType();
            var nameOrDescription = ParameterService.GetParameterDescription(member) ??
                                    ParameterService.GetParameterMemberName(member);
            var text = $"{nameOrDescription.TrimEnd('.')}:";

            while (member.GetValue(build) == null)
            {
                var valueSet = ParameterService.GetParameterValueSet(member, build);
                var value    = valueSet == null
                    ? ConsoleUtility.PromptForInput(text, defaultValue : null)
                    : ConsoleUtility.PromptForChoice(text, valueSet.Select(x => (x.Object, x.Text)).ToArray());

                member.SetValue(build, ReflectionUtility.Convert(value, memberType));
            }
        }
Пример #3
0
        public PropertyInfo(TypeInfo typeInfo, System.Reflection.PropertyInfo info)
            : base(typeInfo, info, null, typeInfo.TypeCache.GetTypeInfo(info.PropertyType))
        {
            this.callGet = new Lazy <Func <object, object> >(() =>
            {
                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                MemberExpression propertyAccess = Expression.MakeMemberAccess(castTarget, info);
                Expression castProperty         = Expression.Convert(propertyAccess, typeof(object));

                return(ReflectionUtility.CompileLambda <Func <object, object> >(castProperty, targetParam));
            });

            this.callSet = new Lazy <Action <object, object> >(() =>
            {
                System.Reflection.MethodInfo setMethod = info.GetSetMethod(nonPublic: true);
                if (setMethod == null)
                {
                    throw JsonException.New(Resources.Convert_NoSetter, info.Name, info.DeclaringType.FullName);
                }

                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                ParameterExpression valueParam  = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression castValue            = Expression.Convert(valueParam, info.PropertyType);
                Expression propertySet          = Expression.Call(castTarget, setMethod, castValue);

                return(ReflectionUtility.CompileLambda <Action <object, object> >(propertySet, targetParam, valueParam));
            });
        }
Пример #4
0
        private DictionaryInfo(TypeInfo typeInfo, System.Reflection.PropertyInfo info, TypeInfo keyType, TypeInfo valueType)
            : base(typeInfo, info, keyType, valueType)
        {
            this.info = info;

            this.callTryGetValue = new Lazy <TryGetValueDelegate>(() =>
            {
                System.Reflection.MethodInfo tryGetMethod = typeInfo.Type.GetMethod("TryGetValue");
                if (tryGetMethod == null)
                {
                    return(null);
                }

                ParameterExpression targetParam   = Expression.Parameter(typeof(object), null);
                ParameterExpression keyParam      = Expression.Parameter(typeof(object), null);
                ParameterExpression outValueParam = Expression.Parameter(typeof(object).MakeByRefType(), null);
                Expression castTarget             = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression castKey    = ReflectionUtility.Convert(keyParam, keyType);
                Expression callTryGet = Expression.Call(castTarget, tryGetMethod, castKey, outValueParam);

                return(ReflectionUtility.CompileLambda <TryGetValueDelegate>(callTryGet, targetParam, keyParam, outValueParam));
            });

            this.callGet = new Lazy <Func <object, object, object> >(() =>
            {
                System.Reflection.MethodInfo getMethod = info.GetGetMethod(nonPublic: false);
                if (getMethod == null)
                {
                    return(null);
                }

                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                ParameterExpression keyParam    = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression castKey = ReflectionUtility.Convert(keyParam, keyType);
                Expression callGet = Expression.Call(castTarget, getMethod, castKey);

                return(ReflectionUtility.CompileLambda <Func <object, object, object> >(callGet, targetParam, keyParam));
            });

            this.callSet = new Lazy <Action <object, object, object> >(() =>
            {
                System.Reflection.MethodInfo setMethod = info.GetSetMethod(nonPublic: false);
                if (setMethod == null)
                {
                    return(null);
                }

                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                ParameterExpression keyParam    = Expression.Parameter(typeof(object), null);
                ParameterExpression valueParam  = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression castKey   = ReflectionUtility.Convert(keyParam, keyType);
                Expression castValue = ReflectionUtility.Convert(valueParam, valueType);
                Expression callSet   = Expression.Call(castTarget, setMethod, castKey, castValue);

                return(ReflectionUtility.CompileLambda <Action <object, object, object> >(callSet, targetParam, keyParam, valueParam));
            });
        }
Пример #5
0
        private CollectionInfo(TypeInfo typeInfo, System.Reflection.PropertyInfo info, System.Reflection.MethodInfo addMethod, TypeInfo valueType)
            : base(typeInfo, info, null, valueType)
        {
            this.callAdd = new Lazy <Action <object, object> >(() =>
            {
                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                ParameterExpression valueParam  = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression castValue            = ReflectionUtility.Convert(valueParam, valueType);
                Expression callAdd = Expression.Call(castTarget, addMethod, castValue);

                return(ReflectionUtility.CompileLambda <Action <object, object> >(callAdd, targetParam, valueParam));
            });
        }
Пример #6
0
        private KvpCollectionInfo(
            TypeInfo typeInfo,
            System.Reflection.PropertyInfo info,
            System.Reflection.MethodInfo addMethod,
            bool mustBeKvp,
            TypeInfo kvpType,
            TypeInfo keyType,
            TypeInfo valueType)
            : base(typeInfo, info, keyType, valueType)
        {
            this.mustBeKvp = mustBeKvp;

            this.constructKvp = new Lazy <Func <object, object, object> >(() =>
            {
                System.Reflection.ConstructorInfo kvpConstructor = kvpType.Type.GetConstructor(new Type[] { keyType.Type, valueType.Type });

                ParameterExpression keyParam   = Expression.Parameter(typeof(object), null);
                ParameterExpression valueParam = Expression.Parameter(typeof(object), null);
                Expression castKey             = ReflectionUtility.Convert(keyParam, keyType);
                Expression castValue           = ReflectionUtility.Convert(valueParam, valueType);
                Expression newKvp     = Expression.New(kvpConstructor, castKey, castValue);
                Expression convertKvp = Expression.Convert(newKvp, typeof(object));

                return(ReflectionUtility.CompileLambda <Func <object, object, object> >(convertKvp, keyParam, valueParam));
            });

            this.callAdd = new Lazy <Action <object, object> >(() =>
            {
                ParameterExpression targetParam = Expression.Parameter(typeof(object), null);
                ParameterExpression valueParam  = Expression.Parameter(typeof(object), null);
                Expression castTarget           = ReflectionUtility.Convert(targetParam, typeInfo);
                Expression castValue            = ReflectionUtility.Convert(valueParam, mustBeKvp ? kvpType : valueType);
                Expression callAdd = Expression.Call(castTarget, addMethod, castValue);

                return(ReflectionUtility.CompileLambda <Action <object, object> >(callAdd, targetParam, valueParam));
            });
        }