コード例 #1
0
        public static void SetValue(
#if !NET20
            this
#endif
            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 (Value != null && propertyInfo.PropertyType != Value.GetType())
                    {
                        value = MyTypeHelper.ConvertTo(value, propertyInfo.PropertyType);
                    }
                    propertyInfo.SetValue(Obj, value, null);
                }
            }
        }
コード例 #2
0
        ////////////////////////////////////////

        public static DynamicCallResult CallMethod(
#if !NET20
            this
#endif
            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>();

                if (parametersInfo.Count == 1 &&
                    parametersInfo[0].ParameterType == typeof(DynLanMethodParameters))
                {
                    foreach (Object parameter in Parameters)
                    {
                        finalParameters.Add(parameter);
                    }

                    finalParameters = new List <object>()
                    {
                        new DynLanMethodParameters()
                        {
                            Parameters = finalParameters.ToArray()
                        }
                    };
                }
                else
                {
                    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);
        }
コード例 #3
0
ファイル: MyTypeHelper.cs プロジェクト: fengweijp/DynLan
 public static Type GetType(Object Value1, Object Value2)
 {
     if (SameType(Value1, Value2))
     {
         if (Value1 != null)
         {
             return(MyTypeHelper.GetNonNullableType(Value1.GetType()));
         }
     }
     return(null);
 }
コード例 #4
0
ファイル: MyTypeHelper.cs プロジェクト: fengweijp/DynLan
        ////////////////////

        public static Boolean IsEnumerable(
#if !NET20
            this
#endif
            Type Type)
        {
#if !NET20
            return(Type.Is(typeof(IEnumerable)));
#else
            return(MyTypeHelper.Is(Type, typeof(IEnumerable)));
#endif
        }
コード例 #5
0
        public static DynamicCallResult CallMethod(Object Obj, MethodInfo methodInfo, IList <Object> Parameters)
        {
            if (methodInfo != null)
            {
                IList <ParameterInfo> parametersInfo = methodInfo.GetParameters();

                List <Object> finalParameters = new List <Object>();
                Int32         index           = -1;

                if (parametersInfo.Count == 1 &&
                    parametersInfo[0].ParameterType == typeof(DynLanMethodParameters))
                {
                    foreach (Object parameter in Parameters)
                    {
                        finalParameters.Add(parameter);
                    }

                    finalParameters = new List <object>()
                    {
                        new DynLanMethodParameters()
                        {
                            Parameters = finalParameters.ToArray()
                        }
                    };
                }
                else
                {
                    foreach (ParameterInfo parameterInfo in parametersInfo)
                    {
                        index++;

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

                        finalParameters.Add(val);
                    }
                }

                return(new DynamicCallResult()
                {
                    Value = methodInfo.Invoke(Obj, finalParameters.ToArray())
                });
            }
            return(null);
        }
コード例 #6
0
ファイル: MyTypeHelper.cs プロジェクト: fengweijp/DynLan
        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);
        }
コード例 #7
0
ファイル: MyTypeHelper.cs プロジェクト: fengweijp/DynLan
        public static Boolean EqualIn(
#if !NET20
            this
#endif
            Object Object, params Object[] Values)
        {
            if (Values != null)
            {
                foreach (var lValue in Values)
#if !NET20
                { if (Object.IsEqual(lValue))
#else
                    if (MyTypeHelper.IsEqual(Object, lValue))
#endif
                  { return(true); } }
            }
            return(false);
        }
コード例 #8
0
ファイル: RefHelperBase.cs プロジェクト: fengweijp/DynLan
        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);
        }
コード例 #9
0
        //////////////////////////////////////////////////////////////////////////////////

        private static void Init()
        {
            if (cache == null)
            {
                lock (lck)
                {
                    if (cache == null)
                    {
                        cache = new Dictionary <String, Dictionary <String, Type> >();
#if !PCL && !NETCE
                        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                        {
                            var types = assembly.GetTypes();
                            foreach (var type in types)
                            {
                                if (!((type.IsPublic || MyTypeHelper.IsStatic(type)) && !type.IsInterface))
                                {
                                    continue;
                                }

                                var typeName = GetShortName(type.FullName);
                                if (typeName == null)
                                {
                                    continue;
                                }

                                if (!cache.ContainsKey(typeName))
                                {
                                    cache[typeName] = new Dictionary <String, Type>();
                                }

                                cache[typeName][type.Name] = type;
                            }
                        }
#endif
                    }
                }
            }
        }
コード例 #10
0
        public void CopyTo(Object Source, Object Dest, Boolean CopyCollections)
        {
            if (Source == null || Dest == null)
            {
                return;
            }

            foreach (var property in GetPropertyinfos(Source))
            {
                if (!CopyCollections)
                {
                    Type propertyType = property.PropertyType;
                    if (propertyType != null && propertyType != typeof(String) && MyTypeHelper.IsEnumerable(propertyType))
                    {
                        continue;
                    }
                }

                if (Attribute.IsDefined(property, typeof(NoCloneAttribute)))
                {
                    continue;
                }

                try
                {
                    SetValue(
                        Dest,
                        property.Name,
                        GetValue(Source, property.Name));
                }
                catch
                {
                    throw;
                }
            }
        }