Example #1
0
 public FastPropertyLookup(string name, TypeCode typeCode, ReflectionHelpers.LateBoundMethod valueLookup)
 {
     Name         = name;
     ValueLookup  = valueLookup;
     TypeCode     = typeCode;
     NameHashCode = ObjectPropertyList.NameComparer.GetHashCode(name);
 }
 /// <summary>
 /// Register a custom condition method, that can use in condition filters
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="name">Name of the condition filter method</param>
 /// <param name="conditionMethod">Lambda method.</param>
 public static ISetupExtensionsBuilder RegisterConditionMethod(this ISetupExtensionsBuilder setupBuilder, string name, Func <object> conditionMethod)
 {
     if (conditionMethod == null)
     {
         throw new ArgumentNullException(nameof(conditionMethod));
     }
     ReflectionHelpers.LateBoundMethod lateBound = (target, args) => conditionMethod();
     return(RegisterConditionMethod(setupBuilder, name, conditionMethod.Method, lateBound));
 }
Example #3
0
        /// <summary>
        /// Get properties, cached for a type
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> GetProps(object value)
        {
            var type = value.GetType();
            KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> props;

            if (_propsCache.TryGetValue(type, out props))
            {
                if (props.Key.Length != 0 && props.Value.Length == 0)
                {
                    var lateBoundMethods = new ReflectionHelpers.LateBoundMethod[props.Key.Length];
                    for (int i = 0; i < props.Key.Length; i++)
                    {
                        lateBoundMethods[i] = ReflectionHelpers.CreateLateBoundMethod(props.Key[i].GetGetMethod());
                    }
                    props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(props.Key, lateBoundMethods);
                    _propsCache.TryAddValue(type, props);
                }
                return(props);
            }

            PropertyInfo[] properties = null;

            try
            {
                properties = type.GetProperties(PublicProperties);
            }
            catch (Exception ex)
            {
                InternalLogger.Warn(ex, "Failed to get JSON properties for type: {0}", type);
            }
            finally
            {
                if (properties == null)
                {
                    properties = ArrayHelper.Empty <PropertyInfo>();
                }
            }

            // Skip Index-Item-Properties (Ex. public string this[int Index])
            foreach (var prop in properties)
            {
                if (!prop.CanRead || prop.GetIndexParameters().Length != 0 || prop.GetGetMethod() == null)
                {
                    properties = properties.Where(p => p.CanRead && p.GetIndexParameters().Length == 0 && p.GetGetMethod() != null).ToArray();
                    break;
                }
            }

            props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(properties, ArrayHelper.Empty <ReflectionHelpers.LateBoundMethod>());
            _propsCache.TryAddValue(type, props);
            return(props);
        }
        /// <summary>
        /// Initializes the layout renderer.
        /// </summary>
        protected override void InitializeLayoutRenderer()
        {
            base.InitializeLayoutRenderer();
            this.propertyInfo = typeof(Process).GetProperty(this.Property.ToString());
            if (this.propertyInfo == null)
            {
                throw new ArgumentException("Property '" + this.propertyInfo + "' not found in System.Diagnostics.Process");
            }

            lateBoundPropertyGet = ReflectionHelpers.CreateLateBoundMethod(this.propertyInfo.GetGetMethod());

            this.process = Process.GetCurrentProcess();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConditionMethodExpression" /> class.
        /// </summary>
        /// <param name="conditionMethodName">Name of the condition method.</param>
        /// <param name="methodInfo"><see cref="MethodInfo"/> of the condition method.</param>
        /// <param name="lateBoundMethod">Precompiled delegate of the condition method.</param>
        /// <param name="methodParameters">The method parameters.</param>
        public ConditionMethodExpression(string conditionMethodName, MethodInfo methodInfo, ReflectionHelpers.LateBoundMethod lateBoundMethod, IEnumerable <ConditionExpression> methodParameters)
        {
            MethodInfo           = methodInfo;
            _lateBoundMethod     = lateBoundMethod;
            _conditionMethodName = conditionMethodName;
            _methodParameters    = new List <ConditionExpression>(methodParameters).ToArray();
            ParameterInfo[] formalParameters = MethodInfo.GetParameters();
            if (formalParameters.Length > 0 && formalParameters[0].ParameterType == typeof(LogEventInfo))
            {
                _acceptsLogEvent = true;
            }
            _lateBoundMethodDefaultParameters = CreateMethodDefaultParameters(formalParameters, _methodParameters, _acceptsLogEvent ? 1 : 0);

            int actualParameterCount = _methodParameters.Length;

            if (_acceptsLogEvent)
            {
                actualParameterCount++;
            }

            // Count the number of required and optional parameters
            CountParmameters(formalParameters, out var requiredParametersCount, out var optionalParametersCount);

            if (!((actualParameterCount >= requiredParametersCount) && (actualParameterCount <= formalParameters.Length)))
            {
                string message;

                if (optionalParametersCount > 0)
                {
                    message = string.Format(
                        CultureInfo.InvariantCulture,
                        "Condition method '{0}' requires between {1} and {2} parameters, but passed {3}.",
                        conditionMethodName,
                        requiredParametersCount,
                        formalParameters.Length,
                        actualParameterCount);
                }
                else
                {
                    message = string.Format(
                        CultureInfo.InvariantCulture,
                        "Condition method '{0}' requires {1} parameters, but passed {2}.",
                        conditionMethodName,
                        requiredParametersCount,
                        actualParameterCount);
                }
                InternalLogger.Error(message);
                throw new ConditionParseException(message);
            }
        }
        /// <summary>
        /// Get properties, cached for a type
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> GetProps(object value)
        {
            var type = value.GetType();
            KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> props;

            if (_propsCache.TryGetValue(type, out props))
            {
                if (props.Key.Length != 0 && props.Value.Length == 0)
                {
                    var lateBoundMethods = new ReflectionHelpers.LateBoundMethod[props.Key.Length];
                    for (int i = 0; i < props.Key.Length; i++)
                    {
                        lateBoundMethods[i] = ReflectionHelpers.CreateLateBoundMethod(props.Key[i].GetGetMethod());
                    }
                    props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(props.Key, lateBoundMethods);
                    _propsCache.TryAddValue(type, props);
                }
                return(props);
            }

            PropertyInfo[] properties = null;

            try
            {
                properties = type.GetProperties(PublicProperties);
            }
            catch (Exception ex)
            {
                Common.InternalLogger.Warn(ex, "Failed to get JSON properties for type: {0}", type);
            }
            finally
            {
                if (properties == null)
                {
                    properties = ArrayHelper.Empty <PropertyInfo>();
                }
            }

            props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(properties, ArrayHelper.Empty <ReflectionHelpers.LateBoundMethod>());
            _propsCache.TryAddValue(type, props);
            return(props);
        }
Example #7
0
        /// <summary>
        /// Tries to retrieve method-delegate by name.
        /// </summary>
        /// <param name="itemName">The method name.</param>
        /// <param name="result">The result.</param>
        /// <returns>A value of <c>true</c> if the method was found, <c>false</c> otherwise.</returns>
        public bool TryCreateInstance(string itemName, out ReflectionHelpers.LateBoundMethod result)
        {
            lock (_nameToLateBoundMethod)
            {
                if (_nameToLateBoundMethod.TryGetValue(itemName, out result))
                {
                    return(true);
                }
            }

            if (_nameToMethodInfo.TryGetValue(itemName, out var methodInfo))
            {
                result = ReflectionHelpers.CreateLateBoundMethod(methodInfo);
                lock (_nameToLateBoundMethod)
                    _nameToLateBoundMethod[itemName] = result;
                return(true);
            }

            return(false);
        }
        private static FastPropertyLookup[] BuildFastLookup(PropertyInfo[] properties, bool includeType)
        {
            int fastAccessIndex = includeType ? 1 : 0;

            FastPropertyLookup[] fastLookup = new FastPropertyLookup[properties.Length + fastAccessIndex];
            if (includeType)
            {
                fastLookup[0] = new FastPropertyLookup("Type", TypeCode.String, (o, p) => o.GetType().ToString());
            }
            foreach (var prop in properties)
            {
                var  getterMethod = prop.GetGetMethod();
                Type propertyType = getterMethod.ReturnType;
                ReflectionHelpers.LateBoundMethod valueLookup = ReflectionHelpers.CreateLateBoundMethod(getterMethod);
#if NETSTANDARD1_3
                TypeCode typeCode = propertyType == typeof(string) ? TypeCode.String : (propertyType == typeof(int) ? TypeCode.Int32 : TypeCode.Object);
#else
                TypeCode typeCode = Type.GetTypeCode(propertyType); // Skip cyclic-reference checks when not TypeCode.Object
#endif
                fastLookup[fastAccessIndex++] = new FastPropertyLookup(prop.Name, typeCode, valueLookup);
            }
            return(fastLookup);
        }
 private static ISetupExtensionsBuilder RegisterConditionMethod(this ISetupExtensionsBuilder setupBuilder, string name, MethodInfo conditionMethod, ReflectionHelpers.LateBoundMethod lateBoundMethod)
 {
     ConfigurationItemFactory.Default.ConditionMethodDelegates.RegisterDefinition(name, conditionMethod, lateBoundMethod);
     return(setupBuilder);
 }
Example #10
0
 /// <summary>
 /// Registers the definition of a single method.
 /// </summary>
 /// <param name="itemName">The method name.</param>
 /// <param name="itemDefinition">The method info.</param>
 /// <param name="lateBoundMethod">The precompiled method delegate.</param>
 internal void RegisterDefinition(string itemName, MethodInfo itemDefinition, ReflectionHelpers.LateBoundMethod lateBoundMethod)
 {
     _nameToMethodInfo[itemName] = itemDefinition;
     lock (_nameToLateBoundMethod)
         _nameToLateBoundMethod[itemName] = lateBoundMethod;
 }
Example #11
0
        /// <summary>
        /// Initializes the layout renderer.
        /// </summary>
        protected override void InitializeLayoutRenderer()
        {
            base.InitializeLayoutRenderer();
            this.propertyInfo = typeof(Process).GetProperty(this.Property.ToString());
            if (this.propertyInfo == null)
            {
                throw new ArgumentException("Property '" + this.propertyInfo + "' not found in System.Diagnostics.Process");
            }

            lateBoundPropertyGet = ReflectionHelpers.CreateLateBoundMethod(this.propertyInfo.GetGetMethod());

            this.process = Process.GetCurrentProcess();
        }
Example #12
0
 public FastPropertyLookup(string name, TypeCode typeCode, ReflectionHelpers.LateBoundMethod valueLookup)
 {
     Name        = name;
     ValueLookup = valueLookup;
     TypeCode    = typeCode;
 }