示例#1
0
 private static void AssertObjectConfig(IocConfig.ObjectConfig config, string configId)
 {
     if (config == null)
     {
         throw new Exception(string.Concat("找不到id为“", configId, "”的配置项"));
     }
 }
示例#2
0
        private static void SetProperties(IocConfig.ObjectConfig item, object inst)
        {
            List <IocConfig.PropertySetting> props = item.Properties;

            if (props.Count > 0)
            {
                Type instType = inst.GetType();
                foreach (IocConfig.PropertySetting prop in props)
                {
                    PropertyInfo propInfo = instType.GetProperty(prop.Name, BindingFlags.Static |
                                                                 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic |
                                                                 BindingFlags.IgnoreCase);
                    if (propInfo != null && propInfo.CanWrite)
                    {
                        object value = null;
                        if (!string.IsNullOrWhiteSpace(prop.RefId))
                        {
                            //TODO
                        }
                        else
                        {
                            if (propInfo.PropertyType.IsEnum)
                            {
                                value = Enum.Parse(propInfo.PropertyType, prop.Value.ToString(), true);
                            }
                            else
                            {
                                value = Convert.ChangeType(prop.Value, propInfo.PropertyType);
                            }
                        }
                        propInfo.SetValue(inst, value);
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// 根据对象配置信息构建构造参数数组
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private static object[] BuildConstructorArgs(IocConfig.ObjectConfig item)
        {
            if (item.RefConstructorParams.Count > 0)
            {
                List <object> result = new List <object>();
                result.AddRange(item.ConstructorParams);

                foreach (object refItem in item.RefConstructorParams)
                {
                    if (refItem is IocConfig.RefObjectSetting)
                    {
                        IocConfig.RefObjectSetting ros = refItem as IocConfig.RefObjectSetting;
                        object refObj = GetObjectById(ros.RefId);
                        result.Insert(ros.Index, refObj);
                    }
                    else if (refItem is IocConfig.RefListObjectSetting)
                    {
                        IocConfig.RefListObjectSetting rlos = refItem as IocConfig.RefListObjectSetting;

                        rlos.List.Clear();
                        foreach (string itemRefId in rlos.ItemList)
                        {
                            object refObj = GetObjectById(itemRefId);
                            rlos.List.Add(refObj);
                        }

                        if (!rlos.IsArray)
                        {
                            result.Insert(rlos.Index, rlos.List);
                        }
                        else
                        {
                            Type genericType = Type.GetType("System.Object", true, true);

                            Type   listType = rlos.List.GetType();
                            Type[] argTypes = listType.GetGenericArguments();
                            if (argTypes != null && argTypes.Length > 0)
                            {
                                genericType = argTypes[0];
                            }
                            Array paramArray = Array.CreateInstance(genericType, rlos.List.Count);
                            rlos.List.CopyTo(paramArray, 0);

                            result.Insert(rlos.Index, paramArray);
                        }
                    }
                }

                return(result.ToArray());
            }
            else
            {
                return(item.ConstructorParams.ToArray());
            }
        }
示例#4
0
        public static T GetObjectById <T>(string objectId)
        {
            IocConfig.ObjectConfig item = IocConfig.GetObjectConfig(objectId);
            AssertObjectConfig(item, objectId);

            object[] cps    = BuildConstructorArgs(item);
            T        result = GetObject <T>(item.Class, cps);

            SetProperties(item, result);
            return(result);
        }