private static void ReadBoundAttribute(BoundAttributeDescriptorBuilder builder, JObject attribute, JsonSerializer serializer)
        {
            var descriptorKind           = attribute[nameof(BoundAttributeDescriptor.Kind)].Value <string>();
            var name                     = attribute[nameof(BoundAttributeDescriptor.Name)].Value <string>();
            var typeName                 = attribute[nameof(BoundAttributeDescriptor.TypeName)].Value <string>();
            var isEnum                   = attribute[nameof(BoundAttributeDescriptor.IsEnum)].Value <bool>();
            var indexerNamePrefix        = attribute[nameof(BoundAttributeDescriptor.IndexerNamePrefix)].Value <string>();
            var indexerTypeName          = attribute[nameof(BoundAttributeDescriptor.IndexerTypeName)].Value <string>();
            var documentation            = attribute[nameof(BoundAttributeDescriptor.Documentation)].Value <string>();
            var diagnostics              = attribute[nameof(BoundAttributeDescriptor.Diagnostics)].Value <JArray>();
            var metadata                 = attribute[nameof(BoundAttributeDescriptor.Metadata)].Value <JObject>();
            var boundAttributeParameters = attribute[nameof(BoundAttributeDescriptor.BoundAttributeParameters)].Value <JArray>();

            builder.Name          = name;
            builder.TypeName      = typeName;
            builder.Documentation = documentation;

            if (indexerNamePrefix != null)
            {
                builder.AsDictionary(indexerNamePrefix, indexerTypeName);
            }

            if (isEnum)
            {
                builder.IsEnum = true;
            }

            foreach (var diagnostic in diagnostics)
            {
                var diagnosticReader = diagnostic.CreateReader();
                var diagnosticObject = serializer.Deserialize <RazorDiagnostic>(diagnosticReader);
                builder.Diagnostics.Add(diagnosticObject);
            }

            var metadataReader = metadata.CreateReader();
            var metadataValue  = serializer.Deserialize <Dictionary <string, string> >(metadataReader);

            foreach (var item in metadataValue)
            {
                builder.Metadata[item.Key] = item.Value;
            }

            foreach (var boundAttributeParameter in boundAttributeParameters)
            {
                var parameter = boundAttributeParameter.Value <JObject>();
                builder.BindAttributeParameter(b => ReadBoundAttributeParameter(b, parameter, serializer));
            }
        }
예제 #2
0
    private static void ReadBoundAttribute(BoundAttributeDescriptorBuilder builder, JObject attribute, JsonSerializer serializer)
    {
        attribute["Kind"].Value <string>();
        string  name          = attribute["Name"].Value <string>();
        string  typeName      = attribute["TypeName"].Value <string>();
        bool    flag          = attribute["IsEnum"].Value <bool>();
        string  text          = attribute["IndexerNamePrefix"].Value <string>();
        string  valueTypeName = attribute["IndexerTypeName"].Value <string>();
        string  documentation = attribute["Documentation"].Value <string>();
        JArray  jArray        = attribute["Diagnostics"].Value <JArray>();
        JObject jObject       = attribute["Metadata"].Value <JObject>();
        JArray  jArray2       = attribute["BoundAttributeParameters"].Value <JArray>();

        builder.Name          = name;
        builder.TypeName      = typeName;
        builder.Documentation = documentation;
        if (text != null)
        {
            builder.AsDictionary(text, valueTypeName);
        }
        if (flag)
        {
            builder.IsEnum = true;
        }
        foreach (JToken item2 in jArray)
        {
            JsonReader      reader = item2.CreateReader();
            RazorDiagnostic item   = serializer.Deserialize <RazorDiagnostic>(reader);
            builder.Diagnostics.Add(item);
        }
        JsonReader reader2 = jObject.CreateReader();

        foreach (KeyValuePair <string, string> item3 in serializer.Deserialize <Dictionary <string, string> >(reader2))
        {
            builder.Metadata[item3.Key] = item3.Value;
        }
        foreach (JToken item4 in jArray2)
        {
            JObject parameter = item4.Value <JObject>();
            builder.BindAttributeParameter(delegate(BoundAttributeParameterDescriptorBuilder b)
            {
                ReadBoundAttributeParameter(b, parameter, serializer);
            });
        }
    }
    private void ConfigureDictionaryBoundAttribute(
        BoundAttributeDescriptorBuilder builder,
        IPropertySymbol property,
        INamedTypeSymbol containingType,
        AttributeData attributeNameAttribute,
        string attributeName,
        bool hasPublicSetter)
    {
        string dictionaryAttributePrefix    = null;
        var    dictionaryAttributePrefixSet = false;

        if (attributeNameAttribute != null)
        {
            foreach (var argument in attributeNameAttribute.NamedArguments)
            {
                if (argument.Key == TagHelperTypes.HtmlAttributeName.DictionaryAttributePrefix)
                {
                    dictionaryAttributePrefix    = (string)argument.Value.Value;
                    dictionaryAttributePrefixSet = true;
                    break;
                }
            }
        }

        var dictionaryArgumentTypes = GetDictionaryArgumentTypes(property);

        if (dictionaryArgumentTypes != null)
        {
            var prefix = dictionaryAttributePrefix;
            if (attributeNameAttribute == null || !dictionaryAttributePrefixSet)
            {
                prefix = attributeName + "-";
            }

            if (prefix != null)
            {
                var dictionaryValueType     = dictionaryArgumentTypes[1];
                var dictionaryValueTypeName = GetFullName(dictionaryValueType);
                builder.AsDictionary(prefix, dictionaryValueTypeName);
            }
        }

        var dictionaryKeyType = dictionaryArgumentTypes?[0];

        if (dictionaryKeyType?.SpecialType != SpecialType.System_String)
        {
            if (dictionaryAttributePrefix != null)
            {
                // DictionaryAttributePrefix is not supported unless associated with an
                // IDictionary<string, TValue> property.
                var diagnostic = RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull(GetFullName(containingType), property.Name);
                builder.Diagnostics.Add(diagnostic);
            }

            return;
        }
        else if (!hasPublicSetter && attributeNameAttribute != null && !dictionaryAttributePrefixSet)
        {
            // Must set DictionaryAttributePrefix when using HtmlAttributeNameAttribute with a dictionary property
            // that lacks a public setter.
            var diagnostic = RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNull(GetFullName(containingType), property.Name);
            builder.Diagnostics.Add(diagnostic);

            return;
        }
    }