Exemplo n.º 1
0
        public override object ToDict(Type rDictType, Type rKeyType, Type rValueType)
        {
#if ILRUNTIME_USE
            rDictType = rDictType is ILRuntime.Reflection.ILRuntimeWrapperType ? ((ILRuntime.Reflection.ILRuntimeWrapperType)rDictType).CLRType.TypeForCLR : rDictType;
#endif
            if (rDictType.IsGenericType && typeof(IDictionary).IsAssignableFrom(rDictType.GetGenericTypeDefinition()))
            {
                // 特殊处理IDictionary<,>类型
                IDictionary rObject = (IDictionary)ReflectionAssist.CreateInstance(rDictType, BindingFlags.Default);
                foreach (var rItem in this.dict)
                {
                    object rKey   = GetKey_ByString(rKeyType, rItem.Key);
                    object rValue = rItem.Value.ToObject(rValueType);
                    rObject.Add(rKey, rValue);
                }
                return(rObject);
            }
            else if (rDictType.IsGenericType && typeof(IDict).IsAssignableFrom(rDictType.GetGenericTypeDefinition()))
            {
                // 特殊处理IDict<,>的类型
                IDict rObject = (IDict)ReflectionAssist.CreateInstance(rDictType, BindingFlags.Default);
                foreach (var rItem in this.dict)
                {
                    object rKey   = GetKey_ByString(rKeyType, rItem.Key);
                    object rValue = rItem.Value.ToObject(rValueType);
                    rObject.AddObject(rKey, rValue);
                }
                return(rObject);
            }
            return(null);
        }
Exemplo n.º 2
0
 public override object ToDict(Type rDictType, Type rKeyType, Type rValueType)
 {
     rDictType = ITypeRedirect.GetRedirectType(rDictType);
     if (rDictType.IsGenericType && typeof(IDictionary).IsAssignableFrom(rDictType.GetGenericTypeDefinition()))
     {
         // 特殊处理IDictionary<,>类型
         IDictionary rObject = (IDictionary)ReflectionAssist.CreateInstance(rDictType, BindingFlags.Default);
         foreach (var rItem in this.dict)
         {
             object rKey   = GetKey_ByString(rKeyType, rItem.Key);
             object rValue = rItem.Value.ToObject(rValueType);
             rObject.Add(rKey, rValue);
         }
         return(rObject);
     }
     else if (rDictType.IsGenericType && typeof(IDict).IsAssignableFrom(rDictType.GetGenericTypeDefinition()))
     {
         // 特殊处理IDict<,>的类型
         IDict rObject = (IDict)ReflectionAssist.CreateInstance(rDictType, BindingFlags.Default);
         foreach (var rItem in this.dict)
         {
             object rKey   = GetKey_ByString(rKeyType, rItem.Key);
             object rValue = rItem.Value.ToObject(rValueType);
             rObject.AddObject(rKey, rValue);
         }
         return(rObject);
     }
     return(null);
 }
Exemplo n.º 3
0
        public override object ToObject(Type rType)
        {
#if ILRUNTIME_USE
            rType = rType is ILRuntime.Reflection.ILRuntimeWrapperType ? ((ILRuntime.Reflection.ILRuntimeWrapperType)rType).CLRType.TypeForCLR : rType;
#endif
            if (rType.IsGenericType && typeof(IDictionary).IsAssignableFrom(rType.GetGenericTypeDefinition()))
            {
                // 特殊处理IDictionary<,>类型
                IDictionary rObject    = (IDictionary)ReflectionAssist.CreateInstance(rType, ReflectionAssist.flags_all);
                Type[]      rArgsTypes = rType.GetGenericArguments();
                foreach (var rItem in this.dict)
                {
                    object rKey   = GetKey_ByString(rArgsTypes[0], rItem.Key);
                    object rValue = rItem.Value.ToObject(rArgsTypes[1]);
                    rObject.Add(rKey, rValue);
                }
                return(rObject);
            }
            else if (rType.IsGenericType && typeof(IDict).IsAssignableFrom(rType.GetGenericTypeDefinition()))
            {
                // 特殊处理IDict<,>的类型
                IDict  rObject    = (IDict)ReflectionAssist.CreateInstance(rType, ReflectionAssist.flags_all);
                Type[] rArgsTypes = rType.GetGenericArguments();
                foreach (var rItem in this.dict)
                {
                    object rKey   = GetKey_ByString(rArgsTypes[0], rItem.Key);
                    object rValue = rItem.Value.ToObject(rArgsTypes[1]);
                    rObject.AddObject(rKey, rValue);
                }
                return(rObject);
            }
            else if (rType.IsClass)
            {
                BindingFlags rBindFlags = ReflectionAssist.flags_all;
                object       rObject    = ReflectionAssist.CreateInstance(rType, rBindFlags);
                foreach (var rItem in this.dict)
                {
                    Type      rMemberType = null;
                    FieldInfo rFieldInfo  = rType.GetField(rItem.Key, rBindFlags);
                    if (rFieldInfo != null)
                    {
                        rMemberType = rFieldInfo.FieldType;
                        object rValueObj = rItem.Value.ToObject(rMemberType);
                        rFieldInfo.SetValue(rObject, rValueObj);
                        continue;
                    }
                    PropertyInfo rPropInfo = rType.GetProperty(rItem.Key, rBindFlags);
                    if (rPropInfo != null)
                    {
                        rMemberType = rPropInfo.PropertyType;
                        object rValueObj = rItem.Value.ToObject(rMemberType);
                        rPropInfo.SetValue(rObject, rValueObj, null);
                        continue;
                    }
                }
                return(rObject);
            }
            return(null);
        }