Beispiel #1
0
        public void Inject(TemplatePage page)
        {
            if (page == null)
            {
                throw new ArgumentNullException(nameof(page));
            }

            PropertyInfo[] properties = page.GetType().GetRuntimeProperties()
                                        .Where(p =>
            {
                return
                (p.IsDefined(typeof(RazorInjectAttribute)) &&
                 p.GetIndexParameters().Length == 0 &&
                 !p.SetMethod.IsStatic);
            }).ToArray();

            foreach (var property in properties)
            {
                Type   memberType = property.PropertyType;
                object instance   = services.GetRequiredService(memberType);

                FastPropertySetter setter = _propertyCache.GetOrAdd(property, new FastPropertySetter(property));
                setter.SetValue(page, instance);
            }
        }