Exemplo n.º 1
0
        private void addBehaviorClassToXmlElement(XElement behavior, List <XNamespace> namespaces, out List <XAttribute> additionalTagPrefixes)
        {
            additionalTagPrefixes = null;
            if (behavior == null)
            {
                return;
            }

            if (ClassImplementation != null)
            {
                XElement behaviorElement = new XElement(CLASS);
                behavior.Add(behaviorElement);

                Type behaviorType = ClassImplementation.GetType();
                XmlnsDefinitionAttribute defnAttribute = null;
                object[] attribs = behaviorType.Assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute), false);
                if (attribs != null && attribs.Length > 0)
                {
                    defnAttribute = attribs[0] as XmlnsDefinitionAttribute;
                }

                string behaviorNamespace = behaviorType.Namespace;
                string namespaceMapping  = null;
                if (defnAttribute != null)
                {
                    namespaceMapping = defnAttribute.XmlNamespace;
                }
                else
                {
                    namespaceMapping = string.Format("clr-namespace:{0};assembly={1}", behaviorNamespace, behaviorType.Assembly.FullName.Split(',')[0]);
                }

                XNamespace namespaceForCommand = namespaces.FirstOrDefault <XNamespace>(x => x.NamespaceName == namespaceMapping);
                if (namespaceForCommand == null)
                {
                    namespaceForCommand = namespaceMapping;
                    string tagPrefix = behaviorNamespace.Replace('.', '_');

                    if (additionalTagPrefixes == null)
                    {
                        additionalTagPrefixes = new List <XAttribute>();
                    }

                    additionalTagPrefixes.Add(new XAttribute(XNamespace.Xmlns + tagPrefix, namespaceMapping));
                }

                XElement behaviorCommandImplElement = new XElement(namespaceForCommand + behaviorType.Name);
                behaviorElement.Add(behaviorCommandImplElement);

                ISupportsConfiguration supportsConfiguration = ClassImplementation as ISupportsConfiguration;
                if (supportsConfiguration != null)
                {
                    //string configData = null;
                    //try
                    //{
                    //    configData = supportsConfiguration.SaveConfiguration();
                    //}
                    //catch (Exception ex)
                    //{
                    //    Logger.Instance.LogError(ex);
                    //    OnBehaviorClassSaveConfigurationException(new ExceptionEventArgs(ex, null));
                    //}
                    if (!string.IsNullOrWhiteSpace(ConfigData))
                    {
                        ConfigData = ConfigData.Trim();
                        XElement configDataElement = new XElement(CONFIG_DATA);
                        if (ConfigData.StartsWith("<", StringComparison.Ordinal) && ConfigData.EndsWith(">", StringComparison.Ordinal))
                        {
                            try
                            {
                                XDocument xDoc = XDocument.Parse(ConfigData);
                                configDataElement.Add(xDoc.Root);
                            }
                            catch
                            {
                                configDataElement.Value = ConfigData;
                            }
                        }
                        else
                        {
                            configDataElement.Value = ConfigData;
                        }
                        behavior.Add(configDataElement);
                    }
                }
            }
        }