Exemple #1
0
        protected static void AddDefault <T>(string name, T value, XElement xml)
        {
            XElement nestedType = new XElement(RuntimeSerialisationHelper.CleanName(name));

            if (value is IEnumerable)
            {
                foreach (object enumerableProperty in (IEnumerable)value)
                {
                    nestedType.Add(new XElement(RuntimeSerialisationHelper.CleanName(enumerableProperty.GetType().Name), enumerableProperty.ToString()));
                }
            }
            else
            {
                if (RuntimeSerialisationHelper.IsSimpleType(typeof(T)))
                {
                    AddSimpleType(name, value, nestedType);
                }
                else
                {
                    nestedType.Add(XmlSummaryWriter <T> .GetSummary(value));
                }
            }

            xml.Add(nestedType);
        }
Exemple #2
0
        protected static void AddDictionary <TKey, TValue>(string name, IDictionary <TKey, TValue> value, XElement xml)
        {
            XElement nestedType = new XElement(RuntimeSerialisationHelper.CleanName(name));

            if (value == null)
            {
                AddSimpleType(name, null, nestedType);
                return;
            }

            foreach (KeyValuePair <TKey, TValue> pair in value)
            {
                nestedType.Add(RuntimeSerialisationHelper.IsSimpleType(typeof(TValue))
                    ? new XElement(RuntimeSerialisationHelper.CleanName(pair.Key.ToString()), pair.Value.ToString())
                    : new XElement(RuntimeSerialisationHelper.CleanName(pair.Key.ToString()), XmlSummaryWriter <TValue> .GetSummary(pair.Value)));
            }

            xml.Add(nestedType);
        }
Exemple #3
0
        protected static void AddSingleParameterGenericType <T, T2>(string name, object value, XElement xml)
        {
            XElement nestedType = new XElement(RuntimeSerialisationHelper.CleanName(name));

            if (value == null)
            {
                AddSimpleType(name, null, nestedType);
                return;
            }

            if (value is IEnumerable && typeof(T).IsGenericType)
            {
                foreach (T2 enumerableProperty in (IEnumerable)value)
                {
                    nestedType.Add(RuntimeSerialisationHelper.IsSimpleType(typeof(T2))
                        ? new XElement(RuntimeSerialisationHelper.CleanName(enumerableProperty.GetType().Name), ((object)enumerableProperty ?? "null").ToString())
                        : XmlSummaryWriter <T2> .GetSummary(enumerableProperty));
                }
            }
            else if (value is IEnumerable)
            {
                foreach (T2 enumerableProperty in (IEnumerable)value)
                {
                    nestedType.Add(RuntimeSerialisationHelper.IsSimpleType(typeof(T2))
                        ? new XElement(RuntimeSerialisationHelper.CleanName(enumerableProperty.GetType().Name), enumerableProperty.ToString())
                        : XmlSummaryWriter <T2> .GetSummary(enumerableProperty));
                }
            }
            else if (value is T2)
            {
                nestedType.Add(XmlSummaryWriter <T2> .GetSummary((T2)value));
            }
            else if (value is T)
            {
                nestedType.Add(XmlSummaryWriter <T> .GetSummary((T)value));
            }

            if (nestedType.Elements().Count() > 0)
            {
                xml.Add(nestedType);
            }
        }
Exemple #4
0
        /// <summary>
        /// Assigns a delegate for creating a summary if it does not already exist
        /// then returns the results of calling the delegate.
        /// </summary>
        public static XElement GetSummary(T entity)
        {
            if (entity == null)
            {
                return(new XElement(RuntimeSerialisationHelper.CleanName(typeof(T).Name)));
            }

            if (_summaryWriterDelegate == null)
            {
                lock (_syncLock)
                {
                    if (_summaryWriterDelegate == null)
                    {
                        _summaryWriterDelegate = BuildSummaryWriter();
                    }
                }
            }

            return(_summaryWriterDelegate(entity));
        }
Exemple #5
0
        /// <summary>
        /// Creates a populated namespace containing code for a class
        /// that has a GetSummary method for T.
        /// </summary>
        private static CodeNamespace BuildNamespace()
        {
            CodeNamespace       ns  = new CodeNamespace(DynamicNamespace);
            CodeTypeDeclaration cls = new CodeTypeDeclaration(_dynamicClassName);

            ns.Types.Add(cls);

            cls.BaseTypes.Add(typeof(RuntimeXmlSerialiserBase));

            //public static string GetSummary(T entity)
            CodeMemberMethod method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Static | MemberAttributes.Public;
            method.Name       = "Serialise";
            method.Parameters.Add(new CodeParameterDeclarationExpression(_type, "entity"));
            method.ReturnType = new CodeTypeReference(typeof(XElement));
            cls.Members.Add(method);

            //XElement xElement = new XElement()
            CodeVariableDeclarationStatement initXElement = new CodeVariableDeclarationStatement(
                typeof(XElement), "xElement",
                new CodeObjectCreateExpression(typeof(XElement),
                                               new CodePrimitiveExpression(RuntimeSerialisationHelper.CleanName(typeof(T).Name))));

            method.Statements.Add(initXElement);

            CodeVariableReferenceExpression entity = new CodeVariableReferenceExpression("entity");

            foreach (PropertyInfo prop in _type.GetProperties())
            {
                if (!prop.CanRead)
                {
                    continue;
                }

                if (prop.GetIndexParameters().Any())
                {
                    continue;
                }

                if (prop.GetCustomAttributes(typeof(RuntimeSerialisationIgnoreAttribute), false).Length > 0)
                {
                    continue;
                }

                if (RuntimeSerialisationHelper.IsSimpleType(prop.PropertyType))
                {
                    method.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression(_dynamicClassName),
                            "AddSimpleType",
                            new CodePrimitiveExpression(prop.Name),
                            new CodePropertyReferenceExpression(entity, prop.Name),
                            new CodeVariableReferenceExpression("xElement")));
                }
                else if (prop.PropertyType.IsGenericType && prop.PropertyType.Name.Contains("Dictionary`2"))
                {
                    method.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression(
                                new CodeTypeReferenceExpression(_dynamicClassName),
                                "AddDictionary",
                                new[]
                    {
                        new CodeTypeReference(prop.PropertyType.GetGenericArguments()[0]),
                        new CodeTypeReference(prop.PropertyType.GetGenericArguments()[1])
                    }),
                            new CodePrimitiveExpression(prop.Name),
                            new CodePropertyReferenceExpression(entity, prop.Name),
                            new CodeVariableReferenceExpression("xElement")));
                }
                else if (prop.PropertyType.BaseType != null && prop.PropertyType.BaseType.Name.Contains("Dictionary`2"))
                {
                    method.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression(
                                new CodeTypeReferenceExpression(_dynamicClassName),
                                "AddDictionary",
                                new[]
                    {
                        new CodeTypeReference(prop.PropertyType.BaseType.GetGenericArguments()[0]),
                        new CodeTypeReference(prop.PropertyType.BaseType.GetGenericArguments()[1])
                    }),
                            new CodePrimitiveExpression(prop.Name),
                            new CodePropertyReferenceExpression(entity, prop.Name),
                            new CodeVariableReferenceExpression("xElement")));
                }
                else if (prop.PropertyType.IsGenericType)
                {
                    method.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression(
                                new CodeTypeReferenceExpression(_dynamicClassName),
                                "AddSingleParameterGenericType",
                                new[]
                    {
                        new CodeTypeReference(prop.PropertyType),
                        new CodeTypeReference(prop.PropertyType.GetGenericArguments()[0])
                    }),
                            new CodePrimitiveExpression(prop.Name),
                            new CodePropertyReferenceExpression(entity, prop.Name),
                            new CodeVariableReferenceExpression("xElement")));
                }
                else if (prop.PropertyType.BaseType != null && prop.PropertyType.BaseType == typeof(Array))
                {
                    method.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression(
                                new CodeTypeReferenceExpression(_dynamicClassName),
                                "AddSingleParameterGenericType",
                                new[]
                    {
                        new CodeTypeReference(prop.PropertyType),
                        new CodeTypeReference(prop.PropertyType.FullName.Replace("[]", "")),
                    }),
                            new CodePrimitiveExpression(prop.Name),
                            new CodePropertyReferenceExpression(entity, prop.Name),
                            new CodeVariableReferenceExpression("xElement")));
                }
                else if (prop.PropertyType.BaseType != null && prop.PropertyType.BaseType.IsGenericType)
                {
                    method.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression(
                                new CodeTypeReferenceExpression(_dynamicClassName),
                                "AddSingleParameterGenericType",
                                new[]
                    {
                        new CodeTypeReference(prop.PropertyType),
                        new CodeTypeReference(prop.PropertyType.BaseType.GetGenericArguments()[0]),
                    }),
                            new CodePrimitiveExpression(prop.Name),
                            new CodePropertyReferenceExpression(entity, prop.Name),
                            new CodeVariableReferenceExpression("xElement")));
                }
                else
                {
                    method.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeMethodReferenceExpression(
                                new CodeTypeReferenceExpression(_dynamicClassName),
                                "AddDefault",
                                new[]
                    {
                        new CodeTypeReference(prop.PropertyType),
                    }),
                            new CodePrimitiveExpression(prop.Name),
                            new CodePropertyReferenceExpression(entity, prop.Name),
                            new CodeVariableReferenceExpression("xElement")));
                }
            }

            CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(new CodeVariableReferenceExpression("xElement"));

            method.Statements.Add(returnStatement);

            return(ns);
        }
Exemple #6
0
 protected static void AddSimpleType(string name, object value, XElement xml)
 {
     xml.Add(new XElement(RuntimeSerialisationHelper.CleanName(name), (value ?? "null").ToString()));
 }