Пример #1
0
        public static object Eval(this object o, PropertyInfoEx propertyInfo)
        {
            if (o == null)
            {
                return(null);
            }
            if (propertyInfo == null)
            {
                return(null);
            }

            if (propertyInfo.IsSetGetValueMethed)
            {
                return(propertyInfo.GetValueMethed(o));
            }

            ParameterExpression instance = Expression.Parameter(
                typeof(object), "instance");
            Expression instanceCast = Expression.Convert(
                instance, propertyInfo.PropertyInfo.ReflectedType);
            Expression propertyAccess = Expression.Property(
                instanceCast, propertyInfo.PropertyInfo);
            UnaryExpression castPropertyValue = Expression.Convert(
                propertyAccess, typeof(object));

            Expression <Func <object, object> > lamexpress =
                Expression.Lambda <Func <object, object> >(
                    castPropertyValue, instance);

            propertyInfo.GetValueMethed = lamexpress.Compile();

            return(propertyInfo.GetValueMethed(o));
        }
Пример #2
0
        public PropertyInfoEx GetPropertyInfoEx(string propertyName, bool fullAccess = false)
        {
            PropertyInfoEx pie;

            if (!_propertyInfoMap.TryGetValue(propertyName, out pie))
            {
                PropertyInfo pi = GetPropertyInfo(propertyName, DefaultLookup | BindingFlags.NonPublic);
                if (pi != null)
                {
                    _propertyInfoMap.Add(propertyName, pie = new PropertyInfoEx(pi));
                }
                else
                {
                    // if property not found - search for field with the same name
                    FieldInfo fi = GetFieldInfo(propertyName, DefaultLookup | BindingFlags.NonPublic);
                    if (fi != null)
                    {
                        _propertyInfoMap.Add(propertyName, pie = new PropertyInfoEx(fi));
                    }
                }
            }
            if (pie != null && (fullAccess || pie.IsPublic))
            {
                return(pie);
            }
            return(null);
        }
Пример #3
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var method   = node.Method;
            var property = PropertyInfoEx.Property(method);

            if (property == null)
            {
                return(base.VisitMethodCall(node));
            }
            // rewrite...
            var instance = Visit(node.Object);
            var isSet    = ReferenceEquals(property.SetMethod, method);

            if (!isSet)
            {
                if (node.Arguments.Count == 0)
                {
                    return(instance.Property(property));
                }
                return(instance.Property(property, node.Arguments.Select(Visit)));
            }
            // set method =\
            // last argument is value, other - index
            if (node.Arguments.Count == 1)
            {
                return(instance.Property(property).Assign(Visit(node.Arguments[0])));
            }
            return(instance.Property(property, node.Arguments.Take(node.Arguments.Count - 1).Select(Visit))
                   .Assign(node.Arguments[node.Arguments.Count - 1]));
        }
Пример #4
0
        public void PropertyFromMethodWorks()
        {
            var prop   = typeof(MultiIndexed).Property("Item", typeof(int));
            var method = prop.GetGetMethod();

            Assert.Equal(prop, PropertyInfoEx.Property(method));
            Assert.Null(PropertyInfoEx.Property(typeof(PropertyInfoExTests).GetTypeInfo().GetMethod("PropertyFromMethodWorks")));
        }
Пример #5
0
        public static void SetValue(this object o, PropertyInfoEx property, object val)
        {
            if (property.IsSetSetValueMethed)
            {
                try
                {
                    property.SetValueMethed(o, val);
                }
                catch
                {
                    try
                    {
                        property.SetValueMethed(o, Convert.ChangeType(val, property.PropertyInfo.PropertyType));
                    }
                    catch
                    {
                        throw new Exception(string.Format("字符串转换失败,无法从{0}转到{1}:{2}", val.GetType().Name, property.PropertyInfo.PropertyType.Name, val));
                    }
                }
                return;
            }

            var tp = o.GetType();
            ParameterExpression instance     = Expression.Parameter(typeof(object), "instance");
            ParameterExpression valParameter = Expression.Parameter(typeof(object), "val");

            //转换为真实类型
            var instanceCast     = Expression.Convert(instance, tp);
            var valParameterCast = Expression.Convert(valParameter, property.PropertyInfo.PropertyType);


            var body       = Expression.Call(instanceCast, property.PropertyInfo.GetSetMethod(), valParameterCast);
            var lamexpress = Expression.Lambda <Action <object, object> >(body, instance, valParameter).Compile();

            property.SetValueMethed = lamexpress;
            try
            {
                lamexpress(o, val);
            }
            catch
            {
                try
                {
                    lamexpress(o, Convert.ChangeType(val, property.PropertyInfo.PropertyType));
                }
                catch
                {
                    throw new Exception(string.Format("字符串转换失败,无法从{0}转到{1}:{2}", val.GetType().Name, property.PropertyInfo.PropertyType.Name, val));
                }
            }
        }
Пример #6
0
        public MemberInfo GetPropertyInfo(string propertyName)
        {
            PropertyInfoEx pie = GetPropertyInfoEx(propertyName);

            if (pie == null)
            {
                return(null);
            }
            if (pie.PropertyInfo != null)
            {
                return(pie.PropertyInfo);
            }
            return(pie.FieldInfo);
        }
Пример #7
0
 public Delta <T> SetValues(object values)
 {
     foreach (var p in values.GetType().GetProperties())
     {
         PropertyInfoEx prop = null;
         if (GetProperties().TryGetValue(p.Name, out prop) &&
             p.GetGetMethod() != null)
         {
             //throw new ArgumentException("Member [" + member + "] is not a valid property of Type [" + typeof(T).Name + "]");
             changedValues[p.Name] = ChangeType(p.GetValue(values, null), prop.PropertyInfo.PropertyType);
         }
     }
     return(this);
 }
Пример #8
0
        public DataModelMemberDescriptor(LinqToDB.Mapping.ColumnDescriptor columnDescriptor, ITableDescriptor table)
        {
            Name           = columnDescriptor.MemberName;
            Table          = table;
            PropertyInfoEx = new PropertyInfoEx(columnDescriptor.MemberInfo as PropertyInfo);

            //var propertyInfo = entity.EntityType.GetProperty(property.Name);
            //PropertyInfoEx = new PropertyInfoEx(propertyInfo);

            //if (property.IsCollectionType)
            //    MemberType = EntityMemberType.CollectionProperty;
            ////else if (property.isna) Navigation???
            //else
            //    MemberType = EntityMemberType.ValueProperty;
        }
Пример #9
0
 public Delta <T> SetValues(IDictionary <string, object> values)
 {
     foreach (var item in values)
     {
         PropertyInfoEx prop = null;
         if (GetProperties().TryGetValue(item.Key, out prop) &&
             prop.CanGet)
         {
             //if (IsConvertibleTo(item.Value,prop.PropertyType))
             changedValues[item.Key] = ChangeType(item.Value, prop.PropertyInfo.PropertyType);
             //else
             //    throw new ArgumentException("Ivalid value Member [" + member + "] is not a valid property of Type [" + typeof(T).Name + "]");
         }
     }
     return(this);
 }
Пример #10
0
        //public static object EvalDrect(this object o, PropertyInfo prop)
        //{
        //    if (o == null)
        //        return null;
        //    if (prop == null)
        //        return null;
        //    return prop.GetValue(o, null);
        //}

        public static Func <object, object> GetGetValueFunc(Type type, PropertyInfoEx propertyInfo)
        {
            ParameterExpression instance = Expression.Parameter(
                typeof(object), "instance");
            Expression instanceCast = Expression.Convert(
                instance, propertyInfo.PropertyInfo.ReflectedType);
            Expression propertyAccess = Expression.Property(
                instanceCast, propertyInfo.PropertyInfo);
            UnaryExpression castPropertyValue = Expression.Convert(
                propertyAccess, typeof(object));

            Expression <Func <object, object> > lamexpress =
                Expression.Lambda <Func <object, object> >(
                    castPropertyValue, instance);

            return(lamexpress.Compile());
        }
Пример #11
0
        public static Action <object, object> GetSetValueFunc(Type type, PropertyInfoEx property)
        {
            ParameterExpression instance     = Expression.Parameter(typeof(object), "instance");
            ParameterExpression valParameter = Expression.Parameter(typeof(object), "val");

            //转换为真实类型
            var instanceCast     = Expression.Convert(instance, type);
            var valParameterCast = Expression.Convert(valParameter, property.PropertyInfo.PropertyType);


            var body       = Expression.Call(instanceCast, property.PropertyInfo.GetSetMethod(), valParameterCast);
            var lamexpress = Expression.Lambda <Action <object, object> >(body, instance, valParameter).Compile();

            //property.SetValueMethed = lamexpress;

            return(lamexpress);
        }
Пример #12
0
        public virtual void VisitProperty(object currentSource, object currentClone, PropertyInfoEx prop)
        {
            var propType = prop.PropertyInfo.PropertyType;

            if (!prop.IsEnumerable)
            {
                if (!propType.IsValueType && !propType.IsEnum &&
                    propType != typeof(string))
                {
                    VisitReferenceProperty(currentSource, currentClone, prop);
                }
                else
                {
                    VisitValueProperty(currentSource, currentClone, prop);
                }
            }
            else
            {
                VisitCollectionProperty(currentSource, currentClone, prop);
            }
        }
 public AggregateListPropertyDescriptor(PropertyInfoEx property, Type elementType, bool isReadOnly) : base(property, elementType, isReadOnly)
 {
 }
 public UnsupportedPropertyDescriptor(PropertyInfoEx property) : base(property)
 {
 }
 public ValuePropertyDescriptor(PropertyInfoEx property, DomainValueType domainValueType) : base(property)
 {
     DomainValueType = domainValueType;
 }
Пример #16
0
        public virtual void VisitValueProperty(object currentSource, object currentClone, PropertyInfoEx prop)
        {
            object value = prop.Get(currentSource, true);

            prop.Set(currentClone, value, true);
        }
Пример #17
0
        public virtual void VisitReferenceProperty(object currentSource, object currentClone, PropertyInfoEx prop)
        {
            object value = prop.Get(currentSource, true);

            prop.Set(currentClone, CloneObject(value), true);
        }
Пример #18
0
 public AggregatePropertyDescriptor(PropertyInfoEx property) : base(property)
 {
 }
Пример #19
0
 /// <summary>
 /// Zwraca wartość wskazującą, czy okno zostało usunięte.
 /// </summary>
 /// <param name="window">Okno.</param>
 public static bool IsDisposed(this Window window)
 {
     return(PropertyInfoEx.GetValue <Window, bool>(window, "IsDisposed"));
 }
Пример #20
0
        public ActionResult Index(ReportFilter filter)
        {
            ReportView reportView = new ReportView();

            reportView.Filter = new ReportFilter();
            reportView.Locals = new SelectList(db.Reports.GroupBy(item => item.Local).Select(item => item.Key).OrderBy(item => item));

            if (!string.IsNullOrEmpty(filter.Local) || filter.DateFrom.HasValue || filter.DateTo.HasValue)
            {
                if (filter.DateFrom.HasValue && filter.DateTo.HasValue && filter.DateFrom.Value > filter.DateTo.Value)
                {
                    DateTime tempDateFrom = filter.DateFrom.Value;
                    filter.DateFrom = filter.DateTo;
                    filter.DateTo   = tempDateFrom;
                }

                string sql        = "SELECT * FROM dbo.Report WHERE";
                string conditions = null;
                List <SqlParameter> parameters = new List <SqlParameter>();
                string parameterName           = "";

                if (!string.IsNullOrEmpty(filter.Local))
                {
                    if (!string.IsNullOrEmpty(conditions))
                    {
                        conditions += " AND";
                    }
                    parameterName = PropertyInfoEx.GetPropertyName(() => filter.Local);
                    conditions   += string.Format(" {0} = @{0}", parameterName);
                    parameters.Add(new SqlParameter(parameterName, filter.Local));
                }
                if (filter.DateFrom.HasValue)
                {
                    if (!string.IsNullOrEmpty(conditions))
                    {
                        conditions += " AND";
                    }
                    parameterName = PropertyInfoEx.GetPropertyName(() => filter.DateFrom);
                    conditions   += string.Format(" Date >= @{0}", parameterName);
                    parameters.Add(new SqlParameter(parameterName, filter.DateFrom));
                }
                if (filter.DateTo.HasValue)
                {
                    if (!string.IsNullOrEmpty(conditions))
                    {
                        conditions += " AND";
                    }
                    parameterName = PropertyInfoEx.GetPropertyName(() => filter.DateTo);
                    conditions   += string.Format(" Date <= @{0}", parameterName);
                    parameters.Add(new SqlParameter(parameterName, filter.DateTo));
                }

                sql += conditions;
                reportView.Reports = db.Reports.SqlQuery(sql, parameters.ToArray()).ToList();
            }
            else
            {
                reportView.Reports = db.Reports.ToList();
            }
            return(View(reportView));
        }
 protected CollectionPropertyDescriptor(PropertyInfoEx property, Type elementType, bool isReadOnly) : base(property)
 {
     ElementType = elementType;
     IsReadOnly  = isReadOnly;
 }
Пример #22
0
        public virtual void VisitCollectionProperty(object currentSource, object currentClone, PropertyInfoEx prop)
        {
            var value = prop.Get(currentSource);

            prop.Set(currentClone, ((IEnumerable)value).Clone(), true);
        }
Пример #23
0
 protected PropertyDescriptor(PropertyInfoEx property)
 {
     Property = property;
 }
Пример #24
0
 public ValueListPropertyDescriptor(PropertyInfoEx property, Type elementType, DomainValueType domainValueType, bool isReadOnly) : base(property, elementType, isReadOnly)
 {
     DomainValueType = domainValueType;
 }