Esempio n. 1
0
        public static FaultResponseEventArgs GetFromXml(XElement element)
        {
            XElement changedParamsMessageElement = new XElement(element);

            changedParamsMessageElement.Descendants("int").ToList().ForEach(localElement => localElement.Name = "i4");
            FaultResponseEventArgsContainer result = new FaultResponseEventArgsContainer();
            List <XElement> paramElements          = new List <XElement> {
                changedParamsMessageElement
            };
            List <RPCParamInfo> parameters = RPCParamInfo.GetFromType(typeof(FaultResponseEventArgsContainer), RPCParamInfo.RevtrievalMode.OnlyWithIndexes);

            RPCCommand.GetParameterElementInstance(parameters, 0, paramElements[0], result);
            return(result.Fault);
        }
Esempio n. 2
0
        internal static void GetParameterElementInstance(List <RPCParamInfo> parameters, int i, XElement paramElement, object result)
        {
            RPCParamInfo paramInfo    = parameters[i];
            XElement     valueElement = paramElement.Element("value");

            if (valueElement == null)
            {
                throw new InvalidOperationException(string.Format("Could not find value element for property '{0}'.", paramInfo.PropertyInfo.Name));
            }

            object value = GetValueInstance(valueElement, paramInfo.PropertyInfo.PropertyType);

            paramInfo.PropertyInfo.SetValue(result, value, null);
        }
Esempio n. 3
0
        private static object GetClassInstance(XContainer valueElement, Type valueType)
        {
            XElement structElement = valueElement.Element("struct");

            if (structElement == null)
            {
                throw new InvalidOperationException("Could not find 'struct' element.");
            }

            object result = Activator.CreateInstance(valueType);

            List <RPCParamInfo> members        = RPCParamInfo.GetFromType(valueType, RPCParamInfo.RevtrievalMode.OnlyWithMemberName);
            List <XElement>     memberElements = new List <XElement>(structElement.Elements("member"));

            if (members.Count != memberElements.Count)
            {
                throw new InvalidOperationException("The amount of member elements in the xml does differ from the amount of members declared on type: " + valueType);
            }

            foreach (XElement memberElement in memberElements)
            {
                XElement nameElement = memberElement.Element("name");

                if (nameElement == null)
                {
                    throw new InvalidOperationException("Could not find 'name' element.");
                }

                string       memberName  = nameElement.Value;
                RPCParamInfo memberParam = members.FirstOrDefault(x => string.Compare(x.MemberName, memberName, StringComparison.OrdinalIgnoreCase) == 0);

                if (memberParam == null)
                {
                    throw new InvalidOperationException(string.Format("Could not find member '{0}' on type '{1}'.", memberName, valueType));
                }

                XElement memberValueElement = memberElement.Element("value");

                if (memberValueElement == null)
                {
                    throw new InvalidOperationException("Could not find 'value' element.");
                }

                object memberValue = GetValueInstance(memberValueElement, memberParam.PropertyInfo.PropertyType);
                memberParam.PropertyInfo.SetValue(result, memberValue, null);
            }

            return(result);
        }
Esempio n. 4
0
        private static object GetInstance(Type type, XContainer paramsMessageElement)
        {
            object              result        = Activator.CreateInstance(type);
            List <XElement>     paramElements = new List <XElement>(paramsMessageElement.Elements("param"));
            List <RPCParamInfo> parameters    = RPCParamInfo.GetFromType(type, RPCParamInfo.RevtrievalMode.OnlyWithIndexes);

            if (paramElements.Count != parameters.Count)
            {
                throw new InvalidOperationException("The amount of parameters in the xml does differ from the amount of properties declared on type: " + type);
            }

            for (int i = 0; i < paramElements.Count; i++)
            {
                GetParameterElementInstance(parameters, i, paramElements[i], result);
            }

            return(result);
        }
Esempio n. 5
0
        public static XElement GetValueElement(object value)
        {
            if (value is int || value is uint || value is short | value is ushort || value is byte || value is sbyte || value is long || value is ulong)
            {
                return(new XElement("value", new XElement("int", value)));
            }

            if (value is float || value is double || value is decimal)
            {
                return(new XElement("value", new XElement("double", value)));
            }

            if (value is bool)
            {
                return(new XElement("value", new XElement("boolean", (bool)value ? "1" : "0")));
            }

            if (value == null || value is string)
            {
                return(new XElement("value", new XElement("string", value)));
            }

            if (value is byte[])
            {
                return(new XElement("value", new XElement("base64", Convert.ToBase64String(value as byte[]))));
            }

            if (value is IEnumerable)
            {
                XElement valueElement = new XElement("value");

                XElement arrayElement = new XElement("array");
                valueElement.Add(arrayElement);

                XElement dataElement = new XElement("data");
                arrayElement.Add(dataElement);

                foreach (object childValue in (value as IEnumerable))
                {
                    dataElement.Add((GetValueElement(childValue)));
                }

                return(valueElement);
            }

            if (value is DateTime)
            {
                return(new XElement("value", new XElement("dateTime.iso8601", ((DateTime)value).ToString("yyyyMMddThh:mm:ss"))));
            }

            if (value == null)
            {
                throw new InvalidOperationException("Null values are not supported");
            }

            if (value.GetType().IsClass)
            {
                XElement result        = new XElement("value");
                XElement structElement = new XElement("struct");

                List <RPCParamInfo> members = RPCParamInfo.GetFromType(value.GetType(), RPCParamInfo.RevtrievalMode.OnlyWithMemberName);

                foreach (RPCParamInfo member in members)
                {
                    XElement memberElement = new XElement("member", new XElement("name", member.MemberName));
                    memberElement.Add(GetValueElement(member.PropertyInfo.GetValue(value, null)));

                    structElement.Add(memberElement);
                }

                result.Add(structElement);

                return(result);
            }

            throw new InvalidOperationException("Specified type is not supported: " + value.GetType());
        }