Esempio n. 1
0
        private bool CatelVersionSupportsXmlSchemaManager(CatelType catelType)
        {
            if (catelType is null)
            {
                return(false);
            }

            if (!_isSupported.HasValue)
            {
                if (_msCoreReferenceFinder.XmlQualifiedName is null || _msCoreReferenceFinder.XmlSchemaSet is null)
                {
                    return(false);
                }

                var xmlSchemaManager = (TypeDefinition)catelType.TypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.Xml.XmlSchemaManager");
                _isSupported = xmlSchemaManager != null;

                if (!_isSupported ?? false)
                {
                    var xmlSchemaManagerPre38 = (TypeDefinition)catelType.TypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.XmlSchemaManager");
                    _isSupported = xmlSchemaManagerPre38 != null;
                }
            }

            return(_isSupported.Value);
        }
        public void Execute(CatelType catelType)
        {
            if (_msCoreReferenceFinder.XmlSchemaSet is null || _msCoreReferenceFinder.XmlQualifiedName is null)
            {
                return;
            }

            if (catelType.TypeDefinition.IsAbstract)
            {
                return;
            }

            if (catelType.TypeDefinition.IsEnum)
            {
                return;
            }

            if (!catelType.TypeDefinition.ImplementsCatelModel())
            {
                return;
            }

            if (catelType.TypeDefinition.ImplementsViewModelBase())
            {
                return;
            }

            FodyEnvironment.WriteDebug($"\tExecuting '{GetType().Name}' for '{catelType.TypeDefinition.FullName}'");

            if (AddXmlSchemaProviderAttribute(catelType))
            {
                AddGetXmlSchemaMethod(catelType);
            }
        }
Esempio n. 3
0
        public void Execute(CatelType catelType)
        {
            if (catelType.TypeDefinition.IsAbstract)
            {
                return;
            }

            if (catelType.TypeDefinition.IsEnum)
            {
                return;
            }

            if (!catelType.TypeDefinition.ImplementsCatelModel())
            {
                return;
            }

            if (catelType.TypeDefinition.ImplementsViewModelBase())
            {
                return;
            }

            FodyEnvironment.LogDebug("\t\t Adding xml schema for type " + catelType.TypeDefinition.FullName);

            if (AddXmlSchemaProviderAttribute(catelType))
            {
                AddGetXmlSchemaMethod(catelType);
            }
        }
Esempio n. 4
0
        private bool AddXmlSchemaProviderAttribute(CatelType catelType)
        {
            var catelTypeDefinition = catelType.TypeDefinition;

            var methodName = GetXmlSchemaMethodName(catelType);

            var existingCustomAttribute = (from attribute in catelTypeDefinition.CustomAttributes
                                           where string.Equals(attribute.AttributeType.Name, "XmlSchemaProviderAttribute")
                                           select attribute).FirstOrDefault();

            if (existingCustomAttribute != null)
            {
                var constructorArgument = existingCustomAttribute.ConstructorArguments[0];
                if (string.Equals(constructorArgument.Value, methodName))
                {
                    return(false);
                }
            }

            var xmlSchemaProviderAttribute = catelTypeDefinition.Module.FindType("System.Xml", "System.Xml.Serialization.XmlSchemaProviderAttribute");

            var attributeConstructor = catelTypeDefinition.Module.Import(xmlSchemaProviderAttribute.Resolve().Constructor(false));
            var customAttribute      = new CustomAttribute(attributeConstructor);

            customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(catelTypeDefinition.Module.TypeSystem.String, methodName));

            catelTypeDefinition.CustomAttributes.Add(customAttribute);

            return(true);
        }
Esempio n. 5
0
        private bool AddXmlSchemaProviderAttribute(CatelType catelType)
        {
            var catelTypeDefinition = catelType.TypeDefinition;

            var methodName = GetXmlSchemaMethodName(catelType);

            var existingCustomAttribute = (from attribute in catelTypeDefinition.CustomAttributes
                                           where string.Equals(attribute.AttributeType.Name, "XmlSchemaProviderAttribute")
                                           select attribute).FirstOrDefault();
            if (existingCustomAttribute != null)
            {
                var constructorArgument = existingCustomAttribute.ConstructorArguments[0];
                if (string.Equals(constructorArgument.Value, methodName))
                {
                    return false;
                }
            }

            var xmlSchemaProviderAttribute = catelTypeDefinition.Module.FindType("System.Xml", "System.Xml.Serialization.XmlSchemaProviderAttribute");

            var attributeConstructor = catelTypeDefinition.Module.Import(xmlSchemaProviderAttribute.Resolve().Constructor(false));
            var customAttribute = new CustomAttribute(attributeConstructor);
            customAttribute.ConstructorArguments.Add(new CustomAttributeArgument(catelTypeDefinition.Module.TypeSystem.String, methodName));

            catelTypeDefinition.CustomAttributes.Add(customAttribute);

            return true;
        }
Esempio n. 6
0
        public void Execute(CatelType catelType)
        {
            if (catelType.TypeDefinition.IsAbstract)
            {
                return;
            }

            if (catelType.TypeDefinition.IsEnum)
            {
                return;
            }

            if (!catelType.TypeDefinition.ImplementsCatelModel())
            {
                return;
            }

            if (catelType.TypeDefinition.ImplementsViewModelBase())
            {
                return;
            }

            FodyEnvironment.LogDebug("\t\t Adding xml schema for type " + catelType.TypeDefinition.FullName);

            if (AddXmlSchemaProviderAttribute(catelType))
            {
                AddGetXmlSchemaMethod(catelType);
            }
        }
        private bool CatelVersionSupportsXmlSchemaManager(CatelType catelType)
        {
            if (catelType == null)
            {
                return false;
            }

            if (!_isSupported.HasValue)
            {
                if (_msCoreReferenceFinder.XmlQualifiedName == null || _msCoreReferenceFinder.XmlSchemaSet == null)
                {
                    return false;
                }

                var xmlSchemaManager = (TypeDefinition)catelType.TypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.Xml.XmlSchemaManager");
                _isSupported = xmlSchemaManager != null;

                if (!_isSupported ?? false)
                {
                    var xmlSchemaManagerPre38 = (TypeDefinition)catelType.TypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.XmlSchemaManager");
                    _isSupported = xmlSchemaManagerPre38 != null;
                }
            }

            return _isSupported.Value;
        }
Esempio n. 8
0
        private void ProcessProperty(CatelType catelType, CatelTypeProperty modelProperty, CustomAttribute exposeAttribute)
        {
            var modelName             = modelProperty.Name;
            var viewModelPropertyName = (string)exposeAttribute.ConstructorArguments[0].Value;
            var modelPropertyName     = viewModelPropertyName;

            if (exposeAttribute.ConstructorArguments.Count > 1)
            {
                modelPropertyName = (string)(exposeAttribute.ConstructorArguments[1].Value ?? viewModelPropertyName);
            }

            bool isReadOnly         = false;
            var  isReadOnlyProperty = (from property in exposeAttribute.Properties
                                       where string.Equals(property.Name, "IsReadOnly")
                                       select property).FirstOrDefault();

            if (isReadOnlyProperty.Argument.Value != null)
            {
                isReadOnly = (bool)isReadOnlyProperty.Argument.Value;
            }

            // Check property definition on model
            var modelType          = modelProperty.PropertyDefinition.PropertyType;
            var modelPropertyToMap = modelType.GetProperty(modelPropertyName);

            if (modelPropertyToMap == null)
            {
                FodyEnvironment.LogError($"Exposed property '{modelPropertyName}' does not exist on model '{modelType.FullName}', make sure to set the right mapping");
                return;
            }

            var modelPropertyType = modelPropertyToMap.PropertyType;

            var viewModelPropertyDefinition = new PropertyDefinition(viewModelPropertyName, PropertyAttributes.None, FodyEnvironment.ModuleDefinition.Import(modelPropertyType));

            viewModelPropertyDefinition.DeclaringType = catelType.TypeDefinition;

            catelType.TypeDefinition.Properties.Add(viewModelPropertyDefinition);

            viewModelPropertyDefinition.MarkAsCompilerGenerated(_msCoreReferenceFinder);

            var catelTypeProperty = new CatelTypeProperty(catelType.TypeDefinition, viewModelPropertyDefinition);

            catelTypeProperty.IsReadOnly = isReadOnly;

            var catelPropertyWeaver = new CatelPropertyWeaver(catelType, catelTypeProperty, _msCoreReferenceFinder);

            catelPropertyWeaver.Execute(true);

            var stringType           = _msCoreReferenceFinder.GetCoreTypeReference("String");
            var stringTypeDefinition = catelType.TypeDefinition.Module.Import(stringType);

            var attributeConstructor      = catelType.TypeDefinition.Module.Import(ViewModelToModelAttributeTypeDefinition.Constructor(false));
            var viewModelToModelAttribute = new CustomAttribute(attributeConstructor);

            viewModelToModelAttribute.ConstructorArguments.Add(new CustomAttributeArgument(stringTypeDefinition, modelName));
            viewModelToModelAttribute.ConstructorArguments.Add(new CustomAttributeArgument(stringTypeDefinition, modelPropertyName));
            viewModelPropertyDefinition.CustomAttributes.Add(viewModelToModelAttribute);
        }
Esempio n. 9
0
        private void AddGetXmlSchemaMethod(CatelType catelType)
        {
            var catelTypeDefinition = catelType.TypeDefinition;

            var methodName = GetXmlSchemaMethodName(catelType);

            var alreadyHandled = (from method in catelTypeDefinition.Methods
                                  where method.Name == methodName
                                  select method).Any();
            if (alreadyHandled)
            {
                return;
            }

            var getTypeFromHandle = catelTypeDefinition.Module.GetMethod("GetTypeFromHandle");
            var importedGetTypeFromHandle = catelTypeDefinition.Module.Import(getTypeFromHandle);

            var xmlSchemaManager = (TypeDefinition)catelTypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.Xml.XmlSchemaManager");
            if (xmlSchemaManager == null)
            {
                // Support versions before 3.8
                xmlSchemaManager = (TypeDefinition)catelTypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.XmlSchemaManager");
            }

            var getXmlSchemaMethodOnXmlSchemaManager = catelTypeDefinition.Module.Import(xmlSchemaManager.Methods.First(x => x.IsStatic && x.Name == "GetXmlSchema"));

            //public static XmlQualifiedName GetXmlSchema(XmlSchemaSet schemas)
            //{
            //    Type callingType = typeof(int);
            //    return XmlSchemaManager.GetXmlSchema(callingType, schemas);
            //}

            var getXmlSchemaMethod = new MethodDefinition(methodName, MethodAttributes.Public | MethodAttributes.Static, catelTypeDefinition.Module.Import(_msCoreReferenceFinder.XmlQualifiedName));

            getXmlSchemaMethod.Parameters.Add(new ParameterDefinition("xmlSchemaSet", ParameterAttributes.None, catelTypeDefinition.Module.Import(_msCoreReferenceFinder.XmlSchemaSet)));

            var ldloc1Instruction = Instruction.Create(OpCodes.Ldloc_1);

            var instructions = getXmlSchemaMethod.Body.Instructions;
            instructions.Insert(0, Instruction.Create(OpCodes.Nop),
                                Instruction.Create(OpCodes.Ldtoken, catelTypeDefinition),
                                Instruction.Create(OpCodes.Call, importedGetTypeFromHandle),
                                Instruction.Create(OpCodes.Stloc_0),
                                Instruction.Create(OpCodes.Ldloc_0),
                                Instruction.Create(OpCodes.Ldarg_0),
                                Instruction.Create(OpCodes.Call, getXmlSchemaMethodOnXmlSchemaManager),
                                Instruction.Create(OpCodes.Stloc_1),
                                Instruction.Create(OpCodes.Br_S, ldloc1Instruction),
                                ldloc1Instruction,
                                Instruction.Create(OpCodes.Ret));

            getXmlSchemaMethod.Body.InitLocals = true;
            getXmlSchemaMethod.Body.Variables.Add(new VariableDefinition("callingType", catelTypeDefinition.Module.Import(catelTypeDefinition.Module.FindType("mscorlib", "System.Type"))));
            getXmlSchemaMethod.Body.Variables.Add(new VariableDefinition(catelTypeDefinition.Module.Import(_msCoreReferenceFinder.XmlQualifiedName)));

            catelTypeDefinition.Methods.Add(getXmlSchemaMethod);

            getXmlSchemaMethod.MarkAsCompilerGenerated(_msCoreReferenceFinder);
        }
Esempio n. 10
0
        private void ProcessProperty(CatelType catelType, CatelTypeProperty modelProperty, CustomAttribute exposeAttribute)
        {
            var modelName = modelProperty.Name;
            var viewModelPropertyName = (string)exposeAttribute.ConstructorArguments[0].Value;
            var modelPropertyName = viewModelPropertyName;
            if (exposeAttribute.ConstructorArguments.Count > 1)
            {
                modelPropertyName = (string)(exposeAttribute.ConstructorArguments[1].Value ?? viewModelPropertyName);
            }

            bool isReadOnly = false;
            var isReadOnlyProperty = (from property in exposeAttribute.Properties
                                      where string.Equals(property.Name, "IsReadOnly")
                                      select property).FirstOrDefault();

            if (isReadOnlyProperty.Argument.Value != null)
            {
                isReadOnly = (bool) isReadOnlyProperty.Argument.Value;
            }

            // Check property definition on model
            var modelType = modelProperty.PropertyDefinition.PropertyType;
            var modelPropertyToMap = modelType.GetProperty(modelPropertyName);
            if (modelPropertyToMap == null)
            {
                FodyEnvironment.LogError($"Exposed property '{modelPropertyName}' does not exist on model '{modelType.FullName}', make sure to set the right mapping");
                return;
            }

            var modelPropertyType = modelPropertyToMap.PropertyType;

            var viewModelPropertyDefinition = new PropertyDefinition(viewModelPropertyName, PropertyAttributes.None, FodyEnvironment.ModuleDefinition.Import(modelPropertyType));
            viewModelPropertyDefinition.DeclaringType = catelType.TypeDefinition;

            catelType.TypeDefinition.Properties.Add(viewModelPropertyDefinition);

            viewModelPropertyDefinition.MarkAsCompilerGenerated(_msCoreReferenceFinder);

            var catelTypeProperty = new CatelTypeProperty(catelType.TypeDefinition, viewModelPropertyDefinition);
            catelTypeProperty.IsReadOnly = isReadOnly;

            var catelPropertyWeaver = new CatelPropertyWeaver(catelType, catelTypeProperty, _msCoreReferenceFinder);
            catelPropertyWeaver.Execute(true);

            var stringType = _msCoreReferenceFinder.GetCoreTypeReference("String");
            var stringTypeDefinition = catelType.TypeDefinition.Module.Import(stringType);

            var attributeConstructor = catelType.TypeDefinition.Module.Import(ViewModelToModelAttributeTypeDefinition.Constructor(false));
            var viewModelToModelAttribute = new CustomAttribute(attributeConstructor);
            viewModelToModelAttribute.ConstructorArguments.Add(new CustomAttributeArgument(stringTypeDefinition, modelName));
            viewModelToModelAttribute.ConstructorArguments.Add(new CustomAttributeArgument(stringTypeDefinition, modelPropertyName));
            viewModelPropertyDefinition.CustomAttributes.Add(viewModelToModelAttribute);
        }
Esempio n. 11
0
        private void ProcessType(CatelType catelType)
        {
            foreach (var property in catelType.Properties)
            {
                var propertyDefinition = property.PropertyDefinition;
                var exposeAttributes   = propertyDefinition.GetAttributes("Catel.Fody.ExposeAttribute");
                foreach (var exposeAttribute in exposeAttributes)
                {
                    ProcessProperty(catelType, property, exposeAttribute);
                }

                propertyDefinition.RemoveAttribute("Catel.Fody.ExposeAttribute");
            }
        }
Esempio n. 12
0
        private void ProcessType(CatelType catelType)
        {
            foreach (var property in catelType.Properties)
            {
                var propertyDefinition = property.PropertyDefinition;
                var exposeAttributes = propertyDefinition.GetAttributes("Catel.Fody.ExposeAttribute");
                foreach (var exposeAttribute in exposeAttributes)
                {
                    ProcessProperty(catelType, property, exposeAttribute);
                }

                propertyDefinition.RemoveAttribute("Catel.Fody.ExposeAttribute");
            }
        }
        private void ProcessType(CatelType catelType)
        {
            FodyEnvironment.WriteDebug($"\tExecuting '{GetType().Name}' for '{catelType.TypeDefinition.FullName}'");

            foreach (var property in catelType.Properties)
            {
                var propertyDefinition = property.PropertyDefinition;
                var exposeAttributes   = propertyDefinition.GetAttributes("Catel.Fody.ExposeAttribute");
                foreach (var exposeAttribute in exposeAttributes)
                {
                    ProcessProperty(catelType, property, exposeAttribute);
                }

                propertyDefinition.RemoveAttribute("Catel.Fody.ExposeAttribute");
            }
        }
Esempio n. 14
0
        private void AddGetXmlSchemaMethod(CatelType catelType)
        {
            var catelTypeDefinition = catelType.TypeDefinition;

            var methodName = GetXmlSchemaMethodName(catelType);

            var alreadyHandled = (from method in catelTypeDefinition.Methods
                                  where method.Name == methodName
                                  select method).Any();

            if (alreadyHandled)
            {
                return;
            }

            var getTypeFromHandle         = catelTypeDefinition.Module.GetMethod("GetTypeFromHandle");
            var importedGetTypeFromHandle = catelTypeDefinition.Module.Import(getTypeFromHandle);

            var xmlSchemaManager = (TypeDefinition)catelTypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.Xml.XmlSchemaManager");

            if (xmlSchemaManager == null)
            {
                // Support versions before 3.8
                xmlSchemaManager = (TypeDefinition)catelTypeDefinition.Module.FindType("Catel.Core", "Catel.Runtime.Serialization.XmlSchemaManager");
            }

            var getXmlSchemaMethodOnXmlSchemaManager = catelTypeDefinition.Module.Import(xmlSchemaManager.Methods.First(x => x.IsStatic && x.Name == "GetXmlSchema"));

            //public static XmlQualifiedName GetXmlSchema(XmlSchemaSet schemas)
            //{
            //    Type callingType = typeof(int);
            //    return XmlSchemaManager.GetXmlSchema(callingType, schemas);
            //}

            var getXmlSchemaMethod = new MethodDefinition(methodName, MethodAttributes.Public | MethodAttributes.Static, catelTypeDefinition.Module.Import(_msCoreReferenceFinder.XmlQualifiedName));

            getXmlSchemaMethod.Parameters.Add(new ParameterDefinition("xmlSchemaSet", ParameterAttributes.None, catelTypeDefinition.Module.Import(_msCoreReferenceFinder.XmlSchemaSet)));

            var ldloc1Instruction = Instruction.Create(OpCodes.Ldloc_1);

            var instructions = getXmlSchemaMethod.Body.Instructions;

            instructions.Insert(0, Instruction.Create(OpCodes.Nop),
                                Instruction.Create(OpCodes.Ldtoken, catelTypeDefinition),
                                Instruction.Create(OpCodes.Call, importedGetTypeFromHandle),
                                Instruction.Create(OpCodes.Stloc_0),
                                Instruction.Create(OpCodes.Ldloc_0),
                                Instruction.Create(OpCodes.Ldarg_0),
                                Instruction.Create(OpCodes.Call, getXmlSchemaMethodOnXmlSchemaManager),
                                Instruction.Create(OpCodes.Stloc_1),
                                Instruction.Create(OpCodes.Br_S, ldloc1Instruction),
                                ldloc1Instruction,
                                Instruction.Create(OpCodes.Ret));

            getXmlSchemaMethod.Body.InitLocals = true;
            getXmlSchemaMethod.Body.Variables.Add(new VariableDefinition("callingType", catelTypeDefinition.Module.Import(catelTypeDefinition.Module.FindType("mscorlib", "System.Type"))));
            getXmlSchemaMethod.Body.Variables.Add(new VariableDefinition(catelTypeDefinition.Module.Import(_msCoreReferenceFinder.XmlQualifiedName)));

            catelTypeDefinition.Methods.Add(getXmlSchemaMethod);

            getXmlSchemaMethod.MarkAsCompilerGenerated(_msCoreReferenceFinder);
        }
Esempio n. 15
0
        private string GetXmlSchemaMethodName(CatelType catelType)
        {
            var methodName = $"GetXmlSchemaFor{catelType.TypeDefinition.FullName}";

            return(methodName.Replace(".", string.Empty).Replace("<", string.Empty).Replace(">", string.Empty).Replace("`", string.Empty));
        }
Esempio n. 16
0
 public OnPropertyChangedWeaver(CatelType catelType, MsCoreReferenceFinder msCoreReferenceFinder)
 {
     _catelType = catelType;
     _msCoreReferenceFinder = msCoreReferenceFinder;
 }
 public OnPropertyChangedWeaver(CatelType catelType, MsCoreReferenceFinder msCoreReferenceFinder)
 {
     _catelType             = catelType;
     _msCoreReferenceFinder = msCoreReferenceFinder;
 }
Esempio n. 18
0
        private string GetXmlSchemaMethodName(CatelType catelType)
        {
            var methodName = $"GetXmlSchemaFor{catelType.TypeDefinition.FullName}";

            return methodName.Replace(".", string.Empty).Replace("<", string.Empty).Replace(">", string.Empty).Replace("`", string.Empty);
        }