コード例 #1
0
 private static void ClearExtView(object obj)
 {
     PropertyInfo property = obj.GetType().GetTypeInfo().GetProperty("LinkedView", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
     FieldInfo field = obj.GetType().GetTypeInfo().GetField("_linkedView", BindingFlags.Instance | BindingFlags.NonPublic);
     if (property.GetValue(obj, (object[])null) != null)
         return;
     object obj1 = ViReflectionCache.GetTypeDefaultConstructor(property.PropertyType).Invoke((object[])null);
     field.SetValue(obj, obj1);
 }
コード例 #2
0
 private static void SetExtView(object currentObject, string moPropertyName, IVimClient client, ManagedObjectReference[] moList, Dictionary<string, ObjectContent> objectContentList, Dictionary<string, ViewBase> generatedManagedObjectList, string currentPropertyPath, Dictionary<string, List<string>> allowedPropertyPath)
 {
     ArrayList arrayList = new ArrayList();
     bool flag = false;
     if (moList.Length == 0)
         flag = true;
     FieldInfo field1 = currentObject.GetType().GetTypeInfo().GetField("_linkedView", BindingFlags.Instance | BindingFlags.NonPublic);
     Dictionary<string, PropertyInfo> typeProperties = ViReflectionCache.GetTypeProperties(currentObject.GetType());
     PropertyInfo propertyInfo = (PropertyInfo)null;
     string lower = "LinkedView".ToLower();
     typeProperties.TryGetValue(lower, out propertyInfo);
     FieldInfo field2 = propertyInfo.PropertyType.GetField(moPropertyName, BindingFlags.Instance | BindingFlags.NonPublic);
     bool isArray = field2.FieldType.IsArray;
     Type type = isArray ? field2.FieldType.GetElementType() : field2.FieldType;
     foreach (ManagedObjectReference mo in moList)
     {
         if (objectContentList.ContainsKey(mo.Value) || generatedManagedObjectList.ContainsKey(mo.Value))
         {
             object obj;
             if (generatedManagedObjectList.ContainsKey(mo.Value))
                 obj = (object)generatedManagedObjectList[mo.Value];
             else
                 obj = ViewBase.GetViewType(mo.Type).GetConstructor(new Type[2]
                 {
       typeof (IVimClient),
       typeof (ManagedObjectReference)
                 }).Invoke(new object[2]
                 {
       (object) client,
       (object) mo
                 });
             ViewBase.SetViewData((ViewBase)obj, currentPropertyPath, allowedPropertyPath, objectContentList, generatedManagedObjectList);
             arrayList.Add(obj);
         }
     }
     if (!flag && arrayList.Count <= 0)
         return;
     object obj1 = propertyInfo.GetValue(currentObject, (object[])null) ?? ViReflectionCache.GetTypeDefaultConstructor(propertyInfo.PropertyType).Invoke((object[])null);
     field1.SetValue(currentObject, obj1);
     if (!isArray)
         field2.SetValue(obj1, arrayList[0]);
     else
         field2.SetValue(obj1, (object)arrayList.ToArray(type));
 }
コード例 #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);
        }