示例#1
0
        public static object ToDictionary(this IEnumerable collection, Type dictionaryType)
        {
            ExceptionHelpers.ThrowArgumentExceptionIf("dictionaryType", !TypeSystem.ContainsInterface(dictionaryType, typeof(IDictionary)), Resources.NotValidDictionaryType, new object[0]);
            IDictionary dictionaries = TypeSystem.CreateInstance(dictionaryType) as IDictionary;

            foreach (object obj in collection)
            {
                object propertyValue  = TypeSystem.GetPropertyValue(obj, "Key", true);
                object propertyValue1 = TypeSystem.GetPropertyValue(obj, "Value", true);
                dictionaries.Add(propertyValue, propertyValue1);
            }
            return(dictionaries);
        }
示例#2
0
        internal static object GetValue(ResourceProperty property, object clrObject)
        {
            string name;
            int    num;
            int    num1 = -1;

            string[] strArrays = null;
            if (property.GetCustomState() != null && property.GetCustomState().PsProperty != null)
            {
                char[] chrArray = new char[1];
                chrArray[0] = '.';
                strArrays   = property.GetCustomState().PsProperty.Split(chrArray);
                num1        = 0;
            }
            do
            {
                if (num1 > -1)
                {
                    name = strArrays[num1];
                }
                else
                {
                    name = property.Name;
                }
                string str = name;
                if (clrObject as PSObject == null)
                {
                    clrObject = TypeSystem.GetPropertyValue(clrObject, str, false);
                }
                else
                {
                    PSObject       pSObject = clrObject as PSObject;
                    PSPropertyInfo item     = pSObject.Properties[str];
                    if (item != null)
                    {
                        try
                        {
                            clrObject = item.Value;
                        }
                        catch (GetValueException getValueException1)
                        {
                            GetValueException getValueException = getValueException1;
                            TraceHelper.Current.SerializationPropertyNotFound(str, getValueException.Message);
                            if (!property.ResourceType.IsPrimitive() || property.ResourceType.IsNullable())
                            {
                                clrObject = null;
                            }
                            else
                            {
                                throw new PSObjectSerializationFailedException(string.Format(Resources.PropertyRetrievalFailed, str, getValueException.Message));
                            }
                        }
                    }
                    else
                    {
                        object[] objArray = new object[1];
                        objArray[0] = str;
                        TraceHelper.Current.SerializationPropertyNotFound(str, string.Format(CultureInfo.CurrentCulture, Resources.PropertyNotFoundInPSObject, objArray));
                        if (!property.ResourceType.IsPrimitive() || property.ResourceType.IsNullable())
                        {
                            clrObject = null;
                            break;
                        }
                        else
                        {
                            object[] objArray1 = new object[1];
                            objArray1[0] = str;
                            throw new PSObjectSerializationFailedException(string.Format(CultureInfo.CurrentCulture, Resources.PropertyNotFoundInPSObject, objArray1));
                        }
                    }
                }
                if (clrObject == null || strArrays == null)
                {
                    break;
                }
                num  = num1 + 1;
                num1 = num;
            }while (num < (int)strArrays.Length);
            return(clrObject);
        }