Beispiel #1
0
        private static object GetOrSetFieldOrPropertyValue(object obj, Type objType, string name, Type[] types, object[] index,
                                                           object value, GetOrSet getOrSet, FieldOrProperty fieldOrProperty)
        {
            if (types == null)
            {
                types = new Type[0];
            }

            try {
                for (Type type = objType; type != null; type = type.BaseType)
                {
                    if (getOrSet == GetOrSet.Get)
                    {
                        if (fieldOrProperty == FieldOrProperty.Field)
                        {
                            FieldInfo fieldInfo = type.GetField(name, AllFlags);
                            if (fieldInfo != null)
                            {
                                return(fieldInfo.GetValue(obj));
                            }
                        }
                        else
                        {
                            PropertyInfo propertyInfo = type.GetProperty(name, AllFlags, null, null, types, null);
                            if (propertyInfo != null)
                            {
                                return(propertyInfo.GetValue(obj, index));
                            }
                        }
                    }
                    else
                    {
                        if (fieldOrProperty == FieldOrProperty.Field)
                        {
                            FieldInfo fieldInfo = type.GetField(name, AllFlags);
                            if (fieldInfo != null)
                            {
                                fieldInfo.SetValue(obj, value);
                                return(null);
                            }
                        }
                        else
                        {
                            PropertyInfo propertyInfo = type.GetProperty(name, AllFlags, null, null, types, null);
                            if (propertyInfo != null)
                            {
                                propertyInfo.SetValue(obj, value, index);
                                return(null);
                            }
                        }
                    }
                }
            }
            catch (TargetInvocationException e) {
                ThrowInnerException(e);
            }
            throw new ArgumentException("Cannot find " + fieldOrProperty.ToString().ToLower() + ": " + name, "name");
        }