Ejemplo n.º 1
0
        protected TAttribute ConstructAttributeObject <TAttribute>()
            where TAttribute : PostCompilation.BaseAttribute
        {
            if (TypeDefinition.IsAbstract)
            {
                Globals.Loggers.File.Error("[InstructionBuilders].[Attribute].[ConstructAttributeObject] is throwing exception ([TypeDefinition].[IsAbstract]).");
                throw new InvalidOperationException("TypeDefinition is of abstract type.");
            }

            var attrCtorArgTypes = CustomAttribute.HasConstructorArguments ?
                                   CustomAttribute.ConstructorArguments.Select(a => a.Type.Resolve().GetSystemType()).ToArray() :
                                   new System.Type[0];
            var attrCtorArgValues = CustomAttribute.HasConstructorArguments ?
                                    CustomAttribute.ConstructorArguments.Select(a => a.Value).ToArray() :
                                    new object[0];
            var ctor   = SystemType.GetConstructor(attrCtorArgTypes);
            var result = ctor.Invoke(attrCtorArgValues);
            var type   = result.GetType();

            if (CustomAttribute.HasProperties)
            {
                foreach (var propertyDef in CustomAttribute.Properties)
                {
                    var propertyInfo = type.GetProperty(propertyDef.Name);
                    propertyInfo.SetValue(result, propertyDef.Argument.Value, null);
                }
            }

            return((TAttribute)result);
        }