Beispiel #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);
        }
Beispiel #2
0
        public static void SetValue(this Object Obj, String PropertyName, Object Value)
        {
            if (Obj != null)
            {
                Object value = Value;
                Type   type  = Obj.GetType();
                CacheProperties(type);

                // szukanie property
                PropertyInfo propertyInfo = null;
                if (_propertyCache[type].ContainsKey(PropertyName))
                {
                    propertyInfo = _propertyCache[type][PropertyName];
                }
                else if (_propertyUppercaseCache[type].ContainsKey(PropertyName.ToUpper()))
                {
                    propertyInfo = _propertyUppercaseCache[type][PropertyName.ToUpper()];
                }

                if (propertyInfo != null)
                {
                    if (propertyInfo.PropertyType != Value.GetType())
                    {
                        value = MyTypeHelper.ConvertTo(value, propertyInfo.PropertyType);
                    }
                    propertyInfo.SetValue(Obj, value, null);
                }
            }
        }
Beispiel #3
0
 public static Type GetType(Object Value1, Object Value2)
 {
     if (SameType(Value1, Value2))
     {
         if (Value1 != null)
         {
             return(MyTypeHelper.GetNonNullableType(Value1.GetType()));
         }
     }
     return(null);
 }
Beispiel #4
0
        private static Boolean SameType(Object Value1, Object Value2)
        {
            if (Value1 != null && Value2 != null)
            {
                Type type1 = MyTypeHelper.
                             GetNonNullableType(Value1.GetType());

                Type type2 = MyTypeHelper.
                             GetNonNullableType(Value2.GetType());

                return(type1.Equals(type2));
            }
            else if (Value1 == null && Value2 == null)
            {
                return(true);
            }
            return(false);
        }
Beispiel #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);
        }