Beispiel #1
0
        /// <summary>
        /// 生成字符串
        /// </summary>
        /// <typeparam name="T">数据实体类型</typeparam>
        /// <param name="sc">字符串模板</param>
        /// <param name="de">数据实体实例</param>
        /// <returns>填充数据的字符</returns>
        public static string Compile <T>(string sc, T de, Map map = null) where T : class
        {
            Type            type      = typeof(T);
            List <PropAttr> propAttrs = null;

            if (DataEntityStructures.ContainsKey(type))
            {
                propAttrs = DataEntityStructures[type];
            }
            else
            {
                Recompile(type);
                propAttrs = DataEntityStructures[type];
            }

            for (int i = 0, len = propAttrs.Count; i < len; ++i)
            {
                PropAttr propAttr = propAttrs[i];

                PropertyInfo propInfo = type.GetProperty(propAttr.PropName);
                object       val      = propInfo.GetValue(de, null);
                if ((propInfo.PropertyType == typeof(DateTime) || propInfo.PropertyType == typeof(DateTime?)) && !string.IsNullOrEmpty(propAttr.Format))
                {
                    sc = sc.Replace("@" + propAttr.AttrName, Convert.ToDateTime(val).ToString(propAttr.Format));
                }
                else
                {
                    sc = sc.Replace("@" + propAttr.AttrName, (null != map ? map(propAttr.AttrName, val).ToString() : val.ToString()));
                }
            }

            return(sc);
        }
Beispiel #2
0
        /// <summary>
        /// 预编译数据实体类型结构
        /// </summary>
        /// <param name="type">数据实体类型</param>
        public static void Recompile(Type type)
        {
            List <PropAttr> propAttrs = new List <PropAttr>();

            PropertyInfo[] propInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            for (int i = propInfos.Length - 1; i >= 0; --i)
            {
                PropertyInfo propInfo = propInfos[i];
                object[]     attrs    = propInfo.GetCustomAttributes(typeof(TplVar), false);
                if (null == attrs || 0 == attrs.Length)
                {
                    continue;
                }

                TplVar   tplVarAttr = attrs[0] as TplVar;
                PropAttr propAttr   = new PropAttr();
                propAttr.PropName = propInfo.Name;
                propAttr.AttrName = tplVarAttr.Name;
                propAttr.Format   = tplVarAttr.Format;
                propAttrs.Add(propAttr);
            }

            // 按AttrName内容长度由长到短排序
            propAttrs.Sort(new PropAttrComparer());

            DataEntityStructures[type] = propAttrs;
        }
Beispiel #3
0
    public override void AddCharacterAttr()
    {
        CharacterBaseAttr baseAttr = FactoryManager.attrFactory.GetCharacterBaseAttr(mCharacterID);

        mPrefabName = baseAttr.prefabName;
        IAttrStrategy attrStrategy = new PropAttrStrategy();

        mSpawnPosition = attrStrategy.GetSpawnPosition(mCharacterRefreshPO);
        ICharacterAttr attr = new PropAttr(attrStrategy, baseAttr);

        mCharacter.attr = attr;
        mCharacter.InitRefreshData((E_ActionType)mCharacterRefreshPO.ActionType, mCharacterRefreshPO.AppeareArea, mCharacterRefreshPO.FactorSpeed, mCharacterRefreshPO.DisappearTime);
    }