Ejemplo n.º 1
0
        // static methods
        public static SoapServiceProviderItem Wrap(ServiceProviderItem item)
        {
            if (item == null)
            {
                return(null);
            }

            // wrap only "persistent" properties
            SoapServiceProviderItem sobj = new SoapServiceProviderItem();

            sobj.TypeName = item.GetType().AssemblyQualifiedName;

            // add common properties
            Hashtable props = GetObjectProperties(item, true);

            props.Add("Id", item.Id.ToString());
            props.Add("Name", item.Name);
            props.Add("ServiceId", item.ServiceId.ToString());
            props.Add("PackageId", item.PackageId.ToString());

            List <string> wrProps = new List <string>();

            foreach (string key in props.Keys)
            {
                wrProps.Add(key + "=" + props[key].ToString());
            }

            sobj.Properties = wrProps.ToArray();
            return(sobj);
        }
Ejemplo n.º 2
0
        public static ServiceProviderItem Unwrap(SoapServiceProviderItem sobj)
        {
            Type type = Type.GetType(sobj.TypeName);
            ServiceProviderItem item = (ServiceProviderItem)Activator.CreateInstance(type);

            // get properties
            if (sobj.Properties != null)
            {
                // get type properties and add it to the hash
                Dictionary <string, PropertyInfo> hash = new Dictionary <string, PropertyInfo>();
                PropertyInfo[] propInfos = GetTypeProperties(type);

                foreach (PropertyInfo propInfo in propInfos)
                {
                    hash.Add(propInfo.Name, propInfo);
                }

                // set service item properties
                foreach (string pair in sobj.Properties)
                {
                    try //TODO: that try catch is a dirty fix. Without it we get that issue System.ArgumentException: Object of type 'System.String' cannot be converted to type 'SolidCP.Providers.Virtualization.VirtualHardDiskInfo[]'.
                        //possible that method works only with simple objects.
                    {
                        int    idx  = pair.IndexOf('=');
                        string name = pair.Substring(0, idx);
                        string val  = pair.Substring(idx + 1);
                        if (hash.ContainsKey(name))
                        {
                            // set value
                            PropertyInfo propInfo = hash[name];
                            propInfo.SetValue(item, Cast(val, propInfo.PropertyType), null);
                        }
                    }
                    catch { }
                }
            }

            return(item);
        }