public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (SubClassType == null)
            {
                throw new ArgumentNullException(SubClassTypeName);
            }

            if (!For.Metadata.ModelType.GetTypeInfo().IsAssignableFrom(SubClassType))
            {
                throw new ArgumentException(string.Format(DefaultMessages.NotASubclass, SubClassType.Name, For.Metadata.ModelType.Name), SubClassTypeName);
            }

            if (For.Model != null && !SubClassType.GetTypeInfo().IsAssignableFrom(For.Model.GetType()))
            {
                output.TagName = string.Empty;
                output.Content.SetHtmlContent(string.Empty);
                return;
            }

            var prefix = DerivedClassesRegister.GetCodeFromType(SubClassType);

            prefix         = combinePrefixes(For.Name, prefix);
            output.TagName = string.Empty;
            string childContent;

            using (var scope = new RenderingScope(For.Model, ViewContext.ViewData.GetFullHtmlFieldName(prefix), ViewContext.ViewData))
            {
                childContent = output.Content.IsModified ? output.Content.GetContent() :
                               (await output.GetChildContentAsync()).GetContent();
            }
            output.Content.SetHtmlContent(childContent);
        }
        protected void AdjustColumns()
        {
            if (!options.NeedsRunTimeTypeCheck)
            {
                return;
            }
            var typeInfos = tag.For.Metadata.ModelType.GetTypeInfo();

            foreach (var row in options.Rows)
            {
                foreach (var col in row.VisibleAndHiddenColumns)
                {
                    if (col.AdditionalPrefix != null)
                    {
                        continue;
                    }
                    if (col.For != null && typeInfos.GetProperty(col.For.Metadata.PropertyName) == null)
                    {
                        col.AdditionalPrefix = DerivedClassesRegister.GetCodeFromType(row.For.Metadata.ModelType);
                    }
                    else
                    {
                        col.AdditionalPrefix = string.Empty;
                    }
                }
            }
        }
Пример #3
0
        public override JsonContract ResolveContract(Type type)
        {
            var res = base.ResolveContract(type);

            if (res.Converter == null)
            {
                if (DerivedClassesRegister.GetCodeFromType(type, true) != null)
                {
                    res.Converter = new RuntimeTypesJsonConverter();
                }
            }
            return(res);
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var t = JToken.FromObject(value);

            if (value != null && t.Type == JTokenType.Object)
            {
                var code = DerivedClassesRegister.GetCodeFromType(value.GetType());
                if (code != null)
                {
                    (t as JObject).AddFirst(new JProperty("$type", code));
                }
            }
            t.WriteTo(writer);
        }