Пример #1
0
        ////////////////////////////////////////

        public static DynamicCallResult CallMethod(this Object Obj, String MethodName, IList <Object> Parameters)
        {
            MethodInfo methodInfo = MyReflectionHelper.GetMethod(
                Obj,
                MethodName,
                Parameters == null ? 0 : Parameters.Count);

            if (methodInfo != null)
            {
                IList <ParameterInfo> parametersInfo = methodInfo.GetParameters();

                List <Object> finalParameters = new List <Object>();
                Int32         index           = -1;
                foreach (ParameterInfo parameterInfo in parametersInfo)
                {
                    index++;

                    Object val = MyTypeHelper.ConvertTo(
                        Parameters[index],
                        parameterInfo.ParameterType);

                    finalParameters.Add(val);
                }

                return(new DynamicCallResult()
                {
                    Value = Obj is Type?
                            methodInfo.Invoke(null, finalParameters.ToArray()) :
                                methodInfo.Invoke(Obj, finalParameters.ToArray())
                });
            }
            return(null);
        }
Пример #2
0
        //////////////////////////////////////////////////

        public Object GetValueOrMethod(Object Item, String PropertyName, Int32 ParametersCount, out Boolean FoundValue)
        {
            FoundValue = false;
            var getter = GetGetter(Item, PropertyName);

            if (getter != null)
            {
                FoundValue = true;
                try
                {
                    return(getter(Item));
                }
                catch
                {
                    return(MyReflectionHelper.GetValue(
                               Item,
                               PropertyName));
                }
            }
            else
            {
                var method = MyReflectionHelper.GetMethod(Item, PropertyName, ParametersCount);
                if (method != null)
                {
                    FoundValue = true;
                }
                return(method);
            }
            return(null);
        }
Пример #3
0
        public Object GetValue(Object Item, String PropertyName)
        {
            var getter = GetGetter(Item, PropertyName);

            if (getter != null)
            {
                try
                {
                    return(getter(Item));
                }
                catch
                {
                    return(MyReflectionHelper.GetValue(
                               Item,
                               PropertyName));
                }
            }
            return(null);
        }
Пример #4
0
        public DataType GetValue <DataType>(Object Item, String PropertyName)
        {
            var getter = GetGetter(Item, PropertyName);

            if (getter != null)
            {
                try
                {
                    return((DataType)getter(Item));
                }
                catch
                {
                    return((DataType)MyReflectionHelper.GetValue(
                               Item,
                               PropertyName));
                }
            }
            return(default(DataType));
        }
Пример #5
0
        public bool SetValue <DataType>(Object Item, String PropertyName, DataType Value)
        {
            MemberSetter setter = GetSetter(Item, PropertyName);

            if (setter != null)
            {
                try
                {
                    setter(Item, Value);
                }
                catch
                {
                    Type t1 = GetPropertyType(Item, PropertyName);    // Item.GetType().GetProperty(PropertyName);
                    Type t2 = Value == null ? null : Value.GetType(); // typeof(DataType);

                    if (t2 == null || t1.Equals(t2))
                    {
                        MyReflectionHelper.SetValue(Item, PropertyName, Value);
                    }
                    else
                    {
                        Object newValue = MyTypeHelper.ConvertTo(Value, t1);
                        try
                        {
                            setter(Item, newValue);
                        }
                        catch
                        {
                            MyReflectionHelper.SetValue(Item, PropertyName, newValue);
                        }
                    }

                    throw;
                }
                return(true);
            }
            else
            {
            }
            return(false);
        }