Esempio n. 1
0
        public static PropertyInfo GetTypePropertyByName(Type type, string propName)
        {
            PropertyInfo result;

            ViReflectionCache.GetTypeProperties(type).TryGetValue(propName, out result);
            return(result);
        }
 private static string ValidatePropertyPath(System.Type viewType, string inputProperty)
 {
     System.Type type      = viewType;
     string[]    strArray1 = inputProperty.Split('.');
     string[]    strArray2 = new string[strArray1.Length];
     for (int index = 0; index < strArray1.Length; ++index)
     {
         PropertyInfo propertyInfo = (PropertyInfo)null;
         if (type != (System.Type)null)
         {
             ViReflectionCache.GetTypeProperties(type).TryGetValue(strArray1[index].ToLower(), out propertyInfo);
         }
         if (propertyInfo != (PropertyInfo)null)
         {
             type             = propertyInfo.PropertyType;
             strArray2[index] = VimClient.MakeCamelCase(propertyInfo.Name);
         }
         else
         {
             strArray2[index] = VimClient.MakeCamelCase(strArray1[index]);
             type             = (System.Type)null;
         }
     }
     return(string.Join(".", strArray2));
 }
Esempio n. 3
0
        public static Dictionary <string, PropertyInfo> GetTypeProperties(Type type)
        {
            ViReflectionCache.ReflectionCacheItem reflectionCacheItemByType = ViReflectionCache.GetReflectionCacheItemByType(type);
            Dictionary <string, PropertyInfo>     dictionary = reflectionCacheItemByType.Properties;

            if (dictionary == null)
            {
                PropertyInfo[] expr_19 = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                dictionary = new Dictionary <string, PropertyInfo>(expr_19.Length);
                PropertyInfo[] array = expr_19;
                for (int i = 0; i < array.Length; i++)
                {
                    PropertyInfo propertyInfo = array[i];
                    string       key          = propertyInfo.Name.ToLower();
                    if (!dictionary.ContainsKey(key))
                    {
                        dictionary.Add(key, propertyInfo);
                    }
                    else if (dictionary[key].PropertyType.IsAssignableFrom(propertyInfo.PropertyType))
                    {
                        dictionary[key] = propertyInfo;
                    }
                }
                reflectionCacheItemByType.Properties = dictionary;
            }
            return(dictionary);
        }
 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);
 }
Esempio n. 5
0
        public static ConstructorInfo GetTypeDefaultConstructor(Type type)
        {
            ViReflectionCache.ReflectionCacheItem reflectionCacheItemByType = ViReflectionCache.GetReflectionCacheItemByType(type);
            ConstructorInfo constructorInfo = reflectionCacheItemByType.DefaultConstructor;

            if (constructorInfo == null)
            {
                constructorInfo = type.GetConstructor(Type.EmptyTypes);
                reflectionCacheItemByType.DefaultConstructor = constructorInfo;
            }
            return(constructorInfo);
        }
        private static void SetPropertyTypesAndFixCasing(DynamicPropertyFilterSpecGenerator.PropertyDefinition[] propertyPathList, string startingType)
        {
            Type type = DynamicPropertyFilterSpecGenerator.GetType(startingType);
            int  i    = 0;

            while (i < propertyPathList.Length)
            {
                DynamicPropertyFilterSpecGenerator.PropertyDefinition propertyDefinition = propertyPathList[i];
                if (propertyDefinition.Name == "*")
                {
                    break;
                }
                Dictionary <string, PropertyInfo> arg_3F_0 = ViReflectionCache.GetTypeProperties(type);
                PropertyInfo propertyInfo = null;
                arg_3F_0.TryGetValue(propertyDefinition.Name.ToLower(), out propertyInfo);
                if (propertyInfo == null)
                {
                    throw new ArgumentException(string.Format(Resources.PropertyDoesNotExist, propertyDefinition.Name));
                }
                propertyDefinition.Name = VimClient.MakeCamelCase(propertyInfo.Name);
                if (propertyInfo.PropertyType == typeof(ManagedObjectReference) || propertyInfo.PropertyType == typeof(ManagedObjectReference[]))
                {
                    type = DynamicPropertyFilterSpecGenerator.GetType(string.Format("{0}_LinkedView", type.Name));
                    ViReflectionCache.GetTypeProperties(type).TryGetValue(propertyDefinition.Name.ToLower(), out propertyInfo);
                    if (propertyDefinition.VimType != DynamicPropertyFilterSpecGenerator.VimType.ManagedObject)
                    {
                        propertyDefinition.VimType = DynamicPropertyFilterSpecGenerator.VimType.ManagedObject;
                        goto IL_169;
                    }
                    if (!DynamicPropertyFilterSpecGenerator.GetType(DynamicPropertyFilterSpecGenerator.FixTypeName(propertyInfo.PropertyType.Name)).IsAssignableFrom(DynamicPropertyFilterSpecGenerator.GetType(propertyDefinition.ObjectType)))
                    {
                        throw new ArgumentException(string.Format(Resources.WrongPropertyType, propertyDefinition.Name, propertyInfo.PropertyType.Name));
                    }
                    type = DynamicPropertyFilterSpecGenerator.GetType(propertyDefinition.ObjectType);
                }
                else
                {
                    if (propertyDefinition.VimType == DynamicPropertyFilterSpecGenerator.VimType.ManagedObject)
                    {
                        throw new ArgumentException(string.Format(Resources.SpecifiedDataObjectType, propertyDefinition.Name));
                    }
                    propertyDefinition.VimType = DynamicPropertyFilterSpecGenerator.VimType.DataObject;
                    goto IL_169;
                }
IL_18D:
                i++;
                continue;
IL_169:
                type = DynamicPropertyFilterSpecGenerator.GetType(propertyInfo.PropertyType.Name);
                propertyDefinition.ObjectType = propertyInfo.PropertyType.Name;
                goto IL_18D;
            }
        }
 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));
 }
 private static Type GetType(string typeName)
 {
     return(ViReflectionCache.GetTypeByRemoteTypeAndName(typeof(ManagedObjectReference), DynamicPropertyFilterSpecGenerator.FixTypeName(typeName)));
 }
 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);
     }
 }
Esempio n. 10
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);
        }