Esempio n. 1
0
        public void Inject(ITemplatePage 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();

            var scopeFactory = services.GetRequiredService <IServiceScopeFactory>();

            using (IServiceScope scope = scopeFactory.CreateScope())
            {
                IServiceProvider scopeServices = scope.ServiceProvider;

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

                    FastPropertySetter setter = _propertyCache.GetOrAdd(property, new FastPropertySetter(property));
                    setter.SetValue(page, instance);
                }
            }
        }
Esempio n. 2
0
        public void Ensure_ValueSetter_Initialized()
        {
            var setter = new FastPropertySetter(testPropertyInfo);

            setter.SetValue(testClass, "Go");

            Assert.NotNull(setter.ValueSetter);
        }
Esempio n. 3
0
        public void Ensure_ValueSetter_Sets_Value()
        {
            var    @class   = new TestClass();
            string expected = "Razor";

            var setter = new FastPropertySetter(testPropertyInfo);

            setter.SetValue(@class, expected);

            Assert.Equal(expected, @class.RefProperty);
        }
Esempio n. 4
0
        public void Ensure_ValueSetter_Initialized()
        {
            var setter = new FastPropertySetter(testPropertyInfo);
            var test   = new TestClass();

            string value = "Go";

            setter.SetValue(test, value);

            Assert.NotNull(setter.ValueSetter);
            Assert.Equal(test.RefProperty, value);
        }