internal static void SetViewData(ViewBase currentObject, string currentPropertyPath, Dictionary<string, List<string>> allowedPropertyPath, Dictionary<string, ObjectContent> objectContentList, Dictionary<string, ViewBase> generatedManagedObjectList)
 {
     if (currentPropertyPath == null)
         currentPropertyPath = string.Empty;
     if (!objectContentList.ContainsKey(currentObject.MoRef.Value))
         return;
     if (!generatedManagedObjectList.ContainsKey(currentObject.MoRef.Value))
         generatedManagedObjectList.Add(currentObject.MoRef.Value, currentObject);
     ObjectContent objectContent = objectContentList[currentObject.MoRef.Value];
     if (allowedPropertyPath != null)
         ViewBase.ClearViewData(currentObject, currentPropertyPath, new List<string>((IEnumerable<string>)allowedPropertyPath.Keys));
     else
         ViewBase.ClearViewData(currentObject, currentPropertyPath, (List<string>)null);
     if (objectContent.PropSet != null)
     {
         foreach (DynamicProperty prop in objectContent.PropSet)
         {
             bool flag1 = false;
             string key1 = string.Empty;
             if (allowedPropertyPath != null)
             {
                 key1 = string.Format("{0}{1}{2}", (object)currentPropertyPath, currentPropertyPath != "" ? (object)"." : (object)"", (object)prop.Name.ToLower());
                 string str = string.Format("{0}{1}{2}", (object)currentPropertyPath, currentPropertyPath != "" ? (object)"." : (object)"", (object)"*");
                 bool flag2 = false;
                 bool flag3 = false;
                 foreach (string key2 in allowedPropertyPath.Keys)
                 {
                     if (key2.StartsWith(key1))
                         flag2 = true;
                     if (key2.Equals(str) && ViReflectionCache.GetTypeProperties(currentObject.GetType()).ContainsKey(prop.Name.ToLower()))
                     {
                         flag3 = true;
                         break;
                     }
                     if (key2.Equals(key1))
                         flag3 = true;
                     if (flag2 & flag3)
                         break;
                 }
                 if (!flag3 & flag2)
                     flag1 = true;
                 if (!flag3 && !flag2)
                     continue;
             }
             string[] strArray = prop.Name.Split('.');
             Type type = currentObject.GetType();
             object currentObject1 = (object)currentObject;
             for (int index = 0; index < strArray.Length - 1; ++index)
             {
                 string name = "_" + strArray[index];
                 FieldInfo field = type.GetTypeInfo().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
                 object obj = field.GetValue(currentObject1);
                 type = field.FieldType;
                 if (obj == null)
                 {
                     obj = type.GetConstructor(Type.EmptyTypes).Invoke((object[])null);
                     field.SetValue(currentObject1, obj);
                 }
                 currentObject1 = obj;
             }
             FieldInfo field1 = type.GetField("_" + strArray[strArray.Length - 1], BindingFlags.Instance | BindingFlags.NonPublic);
             if (!flag1)
             {
                 foreach (FieldInfo fieldInfo in ViewBase.GetAllFieldsWithName("_" + strArray[strArray.Length - 1], type))
                     fieldInfo.SetValue(currentObject1, prop.Val);
             }
             ManagedObjectReference[] moList = (ManagedObjectReference[])null;
             if (field1.FieldType == typeof(ManagedObjectReference))
                 moList = new ManagedObjectReference[1]
                 {
       (ManagedObjectReference) prop.Val
                 };
             else if (field1.FieldType == typeof(ManagedObjectReference[]))
             {
                 List<string> stringList = new List<string>();
                 if (allowedPropertyPath != null)
                 {
                     if (allowedPropertyPath.ContainsKey(key1) && allowedPropertyPath[key1] != null)
                         stringList.AddRange((IEnumerable<string>)allowedPropertyPath[key1]);
                     if (allowedPropertyPath.ContainsKey(key1 + "->") && allowedPropertyPath[key1 + "->"] != null)
                         stringList.AddRange((IEnumerable<string>)allowedPropertyPath[key1 + "->"]);
                 }
                 List<ManagedObjectReference> managedObjectReferenceList = new List<ManagedObjectReference>();
                 foreach (ManagedObjectReference managedObjectReference in (ManagedObjectReference[])prop.Val)
                 {
                     if (stringList.Count == 0 || stringList.Contains(managedObjectReference.Type))
                     {
                         managedObjectReferenceList.Add(managedObjectReference);
                     }
                     else
                     {
                         foreach (string typeName in stringList)
                         {
                             if (ViReflectionCache.GetTypeByRemoteTypeAndName(typeof(ManagedObjectReference), typeName).IsAssignableFrom(ViReflectionCache.GetTypeByRemoteTypeAndName(typeof(ManagedObjectReference), managedObjectReference.Type)))
                             {
                                 managedObjectReferenceList.Add(managedObjectReference);
                                 break;
                             }
                         }
                     }
                 }
                 moList = managedObjectReferenceList.ToArray();
             }
             if (moList != null)
             {
                 string str = string.Format("{0}{1}{2}", (object)currentPropertyPath, currentPropertyPath != "" ? (object)"." : (object)"", (object)prop.Name.ToLower());
                 if (ViewBase.IsNonTerminalPath(allowedPropertyPath, str))
                     ViewBase.SetExtView(currentObject1, field1.Name, currentObject._client, moList, objectContentList, generatedManagedObjectList, str, allowedPropertyPath);
             }
         }
     }
     else if (objectContent.MissingSet != null && objectContent.MissingSet.Length != 0)
     {
         MethodFault fault = objectContent.MissingSet[0].Fault.Fault;
         throw new VimException(objectContent.MissingSet[0].Fault.LocalizedMessage, fault);
     }
 }
 private static Type GetType(string typeName)
 {
     return(ViReflectionCache.GetTypeByRemoteTypeAndName(typeof(ManagedObjectReference), DynamicPropertyFilterSpecGenerator.FixTypeName(typeName)));
 }
示例#3
0
        private static object Convert(object source, Type sourceType, Type resultType)
        {
            object obj = null;

            if (source.GetType().GetTypeInfo().FullName == "VMware.Vim.SecurePasswordField")
            {
                source     = ((SecurePasswordField)source).ToPasswordField();
                sourceType = source.GetType();
            }
            if (sourceType == null)
            {
                throw new ArgumentNullException("resultType");
            }
            if (resultType == null)
            {
                throw new ArgumentNullException("resultType");
            }
            if (!resultType.Name.Equals(sourceType.Name))
            {
                throw new InvalidOperationException(string.Format(Resources.InvalidObjectType, sourceType.FullName, resultType.FullName));
            }
            if (source != null)
            {
                if (sourceType == resultType)
                {
                    obj = source;
                }
                else if (sourceType.GetTypeInfo().IsArray)
                {
                    Array     array     = (Array)source;
                    ArrayList arrayList = new ArrayList();
                    for (int i = 0; i < array.Length; i++)
                    {
                        object value = array.GetValue(i);
                        if (value != null)
                        {
                            Type type = value.GetType();
                            Type typeByRemoteTypeAndName = ViReflectionCache.GetTypeByRemoteTypeAndName(resultType, type.Name);
                            if (typeByRemoteTypeAndName != null)
                            {
                                arrayList.Add(VIConvert.Convert(value, type, typeByRemoteTypeAndName));
                            }
                        }
                    }
                    obj = arrayList.ToArray(resultType.GetElementType());
                }
                else if (sourceType.IsEnum)
                {
                    obj = Enum.Parse(resultType, source.ToString(), true);
                }
                else
                {
                    ConstructorInfo typeDefaultConstructor = ViReflectionCache.GetTypeDefaultConstructor(resultType);
                    if (typeDefaultConstructor != null)
                    {
                        obj = typeDefaultConstructor.Invoke(new object[0]);
                        Dictionary <string, PropertyInfo> typeProperties  = ViReflectionCache.GetTypeProperties(sourceType);
                        Dictionary <string, PropertyInfo> typeProperties2 = ViReflectionCache.GetTypeProperties(resultType);
                        foreach (KeyValuePair <string, PropertyInfo> current in typeProperties)
                        {
                            PropertyInfo propertyInfo;
                            typeProperties2.TryGetValue(current.Key, out propertyInfo);
                            if (propertyInfo != null)
                            {
                                object value2 = current.Value.GetValue(source, null);
                                if (value2 != null)
                                {
                                    Type   type2    = value2.GetType();
                                    object value3   = null;
                                    string typeName = type2.Name;
                                    if (type2.GetTypeInfo().FullName == "VMware.Vim.SecurePasswordField")
                                    {
                                        typeName = "PasswordField";
                                    }
                                    Type typeByRemoteTypeAndName2 = ViReflectionCache.GetTypeByRemoteTypeAndName(resultType, typeName);
                                    if (typeByRemoteTypeAndName2 != null)
                                    {
                                        value3 = VIConvert.Convert(value2, type2, typeByRemoteTypeAndName2);
                                    }
                                    bool flag = true;
                                    if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                                    {
                                        PropertyInfo propertyInfo2;
                                        typeProperties.TryGetValue(current.Key + "specified", out propertyInfo2);
                                        if (propertyInfo2 != null)
                                        {
                                            flag = (bool)propertyInfo2.GetValue(source, null);
                                        }
                                    }
                                    if (flag)
                                    {
                                        if (type2.Namespace.Equals(sourceType.Namespace))
                                        {
                                            propertyInfo.SetValue(obj, value3, null);
                                        }
                                        else
                                        {
                                            propertyInfo.SetValue(obj, value2, null);
                                        }
                                    }
                                }
                                if (!propertyInfo.PropertyType.IsByRef)
                                {
                                    PropertyInfo propertyInfo3;
                                    typeProperties2.TryGetValue(propertyInfo.Name.ToLower() + "specified", out propertyInfo3);
                                    if (propertyInfo3 != null)
                                    {
                                        propertyInfo3.SetValue(obj, value2 != null, null);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (obj.GetType().GetTypeInfo().FullName == "VMware.Vim.PasswordField")
            {
                obj = new SecurePasswordField(obj as PasswordField);
            }
            return(obj);
        }