Пример #1
0
        public static object GetPropertyValue(object instance, string propertyName, bool throwIfMissing = true)
        {
            object       value;
            Type         type         = instance.GetType();
            PropertyInfo propertyInfo = TypeSystem.GetPropertyInfo(type, propertyName);

            if (propertyInfo == null)
            {
                object fieldValue = TypeSystem.GetFieldValue(instance, propertyName);
                if (fieldValue != null)
                {
                    return(fieldValue);
                }
            }
            if (!throwIfMissing)
            {
                if (propertyInfo == null)
                {
                    return(null);
                }
            }
            else
            {
                if (propertyInfo == null)
                {
                    object[] assemblyQualifiedName = new object[2];
                    assemblyQualifiedName[0] = propertyName;
                    assemblyQualifiedName[1] = type.AssemblyQualifiedName;
                    throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.PropertyNotFoundInDotNetType, assemblyQualifiedName), "propertyName");
                }
            }
            try
            {
                value = propertyInfo.GetValue(instance, null);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                TraceHelper.Current.DebugMessage(exception.ToTraceMessage(string.Concat("GetPropertyValue failed to parse property: ", propertyName)));
                if (!exception.IsIgnorablePropertyException())
                {
                    if (!exception.IsSevereException())
                    {
                        object[] message = new object[2];
                        message[0] = propertyName;
                        message[1] = exception.Message;
                        throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.PropertyRetrievalFailed, message), exception);
                    }
                    else
                    {
                        throw;
                    }
                }
                else
                {
                    if (!throwIfMissing)
                    {
                        value = null;
                    }
                    else
                    {
                        object[] objArray = new object[2];
                        objArray[0] = propertyName;
                        objArray[1] = type.AssemblyQualifiedName;
                        throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.PropertyNotFoundInDotNetType, objArray), "propertyName");
                    }
                }
            }
            return(value);
        }
Пример #2
0
 public static bool ContainsDictionaryInterface(Type type)
 {
     return(TypeSystem.ContainsInterface(type, typeof(IDictionary)));
 }
Пример #3
0
        private static Type FindIEnumerable(Type seqType)
        {
            Type type;

            if (seqType == null || seqType == typeof(string))
            {
                return(null);
            }
            else
            {
                if (!seqType.IsArray)
                {
                    if (seqType.IsGenericType)
                    {
                        Type[] genericArguments = seqType.GetGenericArguments();
                        int    num = 0;
                        while (num < (int)genericArguments.Length)
                        {
                            Type   type1     = genericArguments[num];
                            Type[] typeArray = new Type[1];
                            typeArray[0] = type1;
                            Type type2 = typeof(IEnumerable <>).MakeGenericType(typeArray);
                            if (!type2.IsAssignableFrom(seqType))
                            {
                                num++;
                            }
                            else
                            {
                                type = type2;
                                return(type);
                            }
                        }
                    }
                    Type[] interfaces = seqType.GetInterfaces();
                    if (interfaces != null && (int)interfaces.Length > 0)
                    {
                        Type[] typeArray1 = interfaces;
                        int    num1       = 0;
                        while (num1 < (int)typeArray1.Length)
                        {
                            Type type3 = typeArray1[num1];
                            Type type4 = TypeSystem.FindIEnumerable(type3);
                            if (type4 == null)
                            {
                                num1++;
                            }
                            else
                            {
                                type = type4;
                                return(type);
                            }
                        }
                    }
                    if (!(seqType.BaseType != null) || !(seqType.BaseType != typeof(object)))
                    {
                        return(null);
                    }
                    else
                    {
                        return(TypeSystem.FindIEnumerable(seqType.BaseType));
                    }
                }
                else
                {
                    Type[] elementType = new Type[1];
                    elementType[0] = seqType.GetElementType();
                    return(typeof(IEnumerable <>).MakeGenericType(elementType));
                }
            }
        }