Esempio n. 1
0
        internal object GetValue(T value, string propertyName)
        {
            var getter = Getters.GetOrAdd(propertyName, name =>
            {
                var propertyInfo = GetType().GetProperty(name);
                return(ResolveGetter(propertyInfo));
            });

            return(getter.Invoke(value));
        }
        private object BindParameter(DisplayContext displayContext, ParameterInfo parameter, TextWriter output)
        {
            if (parameter.Name == "Shape")
            {
                return(displayContext.Value);
            }

            if (parameter.Name == "Display")
            {
                return(displayContext.Display);
            }

            if (parameter.Name == "Output" && parameter.ParameterType == typeof(TextWriter))
            {
                return(output);
            }

            if (parameter.Name == "Output" && parameter.ParameterType == typeof(Action <object>))
            {
                return(new Action <object>(output.Write));
            }

            if (parameter.Name == "Html")
            {
                return(new HtmlHelper(
                           displayContext.ViewContext,
                           displayContext.ViewDataContainer,
                           _routeCollection));
            }

            if (parameter.Name == "Url" && parameter.ParameterType.IsAssignableFrom(typeof(UrlHelper)))
            {
                return(new UrlHelper(displayContext.ViewContext.RequestContext, _routeCollection));
            }

            var getter = Getters.GetOrAdd(parameter.Name, n =>
                                          CallSite <Func <CallSite, object, dynamic> > .Create(
                                              Microsoft.CSharp.RuntimeBinder.Binder.GetMember(
                                                  CSharpBinderFlags.None, n, null, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) })));

            var result = getter.Target(getter, displayContext.Value);

            if (result == null)
            {
                return(null);
            }

            var converter = Converters.GetOrAdd(parameter.ParameterType, CompileConverter);
            var argument  = converter.Invoke(result);

            return(argument);
        }
Esempio n. 3
0
        static (PropertyModification, PropertyModification) GetModificationsOrNull(Type type, string property, object previousValue, object newValue)
        {
            var getter = Getters
                         .GetOrAdd(type, _ => new ConcurrentDictionary <string, Func <object, object, (PropertyModification, PropertyModification)> >())
                         .GetOrAdd(property, _ => CreateModificationGetter(type, property));

            try
            {
                return(getter(previousValue, newValue));
            }
            catch (Exception exception)
            {
                throw new ArgumentException($"Could not generate property modifications for the '{property}' property of {type}, value changed from {previousValue} to {newValue}", exception);
            }
        }