示例#1
0
    public void ProcessType(TypeDefinition type)
    {
        var containsValidationAttribute = type.CustomAttributes.ContainsValidationAttribute();
        if (!type.ImplementsINotify())
        {
            if (containsValidationAttribute)
            {
                throw new WeavingException($"Found [InjectValidationAttribute] on '{type.Name}' but it doesnt implement INotifyPropertyChanged so cannot inject.");
            }
            return;
        }
        if (!containsValidationAttribute)
        {
            return;
        }
        if (type.HasGenericParameters)
        {
            throw new WeavingException($"Failed to process '{type.FullName}'. Generic models are not supported. Feel free to send a pull request.");
        }
        var templateFieldInjector = new TemplateFieldInjector
                                                      {
                                                          ValidationTemplateFinder = templateFinder,
                                                          TargetType = type,
                                                          ModuleDefinition = ModuleDefinition
                                                      };
         templateFieldInjector.AddField();

        if (dataErrorInfoFinder.Found)
        {
            var injector = new DataErrorInfoInjector
            {
                TypeDefinition = type,
                TypeSystem = ModuleDefinition.TypeSystem,
                DataErrorInfoFinder = dataErrorInfoFinder,
                ModuleWeaver = this,
                ValidationTemplateField = templateFieldInjector.ValidationTemplateField
            };
            injector.Execute();
        }

        if (notifyDataErrorInfoFinder.Found)
        {
            var injector = new NotifyDataErrorInfoInjector
            {
                TypeDefinition = type,
                NotifyDataErrorInfoFinder = notifyDataErrorInfoFinder,
                TypeSystem= ModuleDefinition.TypeSystem,
                ModuleWeaver = this,
                ValidationTemplateField = templateFieldInjector.ValidationTemplateField
            };
            injector.Execute();
        }
    }
示例#2
0
    public void ProcessType(TypeDefinition typeDefinition)
    {
        if (!typeDefinition.CustomAttributes.ContainsValidationAttribute())
        {
            return;
        }
        if (typeDefinition.HasGenericParameters)
        {
            throw new WeavingException(string.Format("Failed to process '{0}'. Generic models are not supported. Feel free to send a pull request.", typeDefinition.FullName));
        }
        var templateFieldInjector = new TemplateFieldInjector
                                                      {
                                                          ValidationTemplateFinder = templateFinder,
                                                          TargetType = typeDefinition,
                                                          ModuleDefinition = ModuleDefinition
                                                      };
         templateFieldInjector.AddField();

        if (dataErrorInfoFinder.Found)
        {
            var injector = new DataErrorInfoInjector
            {
                TypeDefinition = typeDefinition,
                TypeSystem = ModuleDefinition.TypeSystem,
                DataErrorInfoFinder = dataErrorInfoFinder,
                ModuleWeaver = this,
                ValidationTemplateField = templateFieldInjector.ValidationTemplateField
            };
            injector.Execute();
        }

        if (notifyDataErrorInfoFinder.Found)
        {
            var injector = new NotifyDataErrorInfoInjector
            {
                TypeDefinition = typeDefinition,
                NotifyDataErrorInfoFinder = notifyDataErrorInfoFinder,
                TypeSystem= ModuleDefinition.TypeSystem,
                ModuleWeaver = this,
                ValidationTemplateField = templateFieldInjector.ValidationTemplateField
            };
            injector.Execute();
        }
    }