Ejemplo n.º 1
0
        public static NeutralAttributeData FromAttributeData(Microsoft.CodeAnalysis.AttributeData attributeData)
        {
            ImmutableArray <object> arguments = attributeData.ConstructorArguments.IsDefaultOrEmpty
                ? ImmutableArray <object> .Empty
                : ImmutableArray.CreateRange(attributeData.ConstructorArguments.Select(a => a.Value));

            string type = attributeData.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) ?? "";

            return(new NeutralAttributeData(type, arguments));
        }
Ejemplo n.º 2
0
        internal static bool Inspect(Microsoft.CodeAnalysis.AttributeData viewModelAttributeData)
        {
            var generateConstructor = true;

            foreach (var arg in viewModelAttributeData.NamedArguments)
            {
                if (arg.Key == "GenerateConstructor")
                {
                    generateConstructor = (bool?)arg.Value.Value == true;
                }
            }

            return(generateConstructor);
        }
Ejemplo n.º 3
0
        public static CGFAttributeData Parse(CGFParserReporter reporter, Microsoft.CodeAnalysis.AttributeData attributeData)
        {
            //TODO - Add way to have custom validation for ArrayAttribute to validate variable name actually matches an serialized variable
            Type attributeType     = Type.GetType(attributeData.AttributeClass.ToString(), false);
            Type baseAttributeType = typeof(CGFAttribute);

            if (!attributeType.IsSubclassOf(baseAttributeType))
            {
                reporter.LogInfo($"Unrecognized attribute type {attributeType.Name}. Attributes are expected to be subtypes of {baseAttributeType.Name}.");
                return(null);
            }

            Attribute attribute = null;

            using (reporter.CreateContextScope(CGFParserReporterContext.Type.Type, attributeType.Name))
            {
                System.Reflection.ConstructorInfo[] constructors = attributeType.GetConstructors();
                foreach (System.Reflection.ConstructorInfo constructorInfo in constructors)
                {
                    System.Reflection.ParameterInfo[] parameters = constructorInfo.GetParameters();
                    if (attributeData.ConstructorArguments.Length != parameters.Length)
                    {
                        continue;
                    }

                    bool matchingParameters = true;
                    for (int paramIndex = 0; paramIndex < parameters.Length; ++paramIndex)
                    {
                        Microsoft.CodeAnalysis.TypedConstant cgfTypedConstant = attributeData.ConstructorArguments[paramIndex];
                        System.Reflection.ParameterInfo      parameterInfo    = parameters[paramIndex];

                        Type cgfParameterType = null;
                        if (string.IsNullOrEmpty(cgfTypedConstant.Type.Name))
                        {
                            cgfParameterType = Type.GetType(cgfTypedConstant.Type.Name, false);
                        }
                        else
                        {
                            cgfParameterType = Type.GetType($"{cgfTypedConstant.Type.ContainingSymbol.Name}.{cgfTypedConstant.Type.Name}", false);
                        }

                        if (cgfParameterType != parameterInfo.ParameterType)
                        {
                            matchingParameters = false;
                            break;
                        }
                    }

                    if (matchingParameters)
                    {
                        if (parameters.Length > 0)
                        {
                            Object[] parameterObjects = new Object[parameters.Length];
                            for (int paramIndex = 0; paramIndex < parameters.Length; ++paramIndex)
                            {
                                Microsoft.CodeAnalysis.TypedConstant cgfTypedConstant = attributeData.ConstructorArguments[paramIndex];
                                System.Reflection.ParameterInfo      parameterInfo    = parameters[paramIndex];

                                Object convertedParam = null;
                                if (parameterInfo.ParameterType.IsEnum)
                                {
                                    convertedParam = Enum.ToObject(parameterInfo.ParameterType, cgfTypedConstant.Value);
                                }
                                else
                                {
                                    convertedParam = System.Convert.ChangeType(cgfTypedConstant.Value, parameterInfo.ParameterType);
                                }

                                parameterObjects[paramIndex] = convertedParam;
                            }

                            attribute = Activator.CreateInstance(attributeType, parameterObjects) as Attribute;
                        }
                        else
                        {
                            attribute = Activator.CreateInstance(attributeType) as Attribute;
                        }
                    }
                }

                if (attribute == null)
                {
                    //TODO - Add provided constructor param list to help finding the error.
                    //       List most likely constructors.
                    reporter.LogError($"No matching constructor found for {attributeType.Name}.");
                    return(null);
                }

                CGFAttributeData CGFAttributeData = new CGFAttributeData(attribute, attributeData);
                return(CGFAttributeData);
            }
        }
Ejemplo n.º 4
0
 CGFAttributeData(Attribute attribute, Microsoft.CodeAnalysis.AttributeData attributeData)
 {
     Attribute       = attribute;
     m_AttributeData = attributeData;
 }