Esempio n. 1
0
        private void setData(CmdAttrMd md, T t, string value)
        {
            // list type
            if (md.listMd != null)
            {
                if (md.listMd.funAdd == null || md.listMd.itemType == null)
                {
                    return;
                }

                try {
                    //var lastArrayMd = md;

                    var lastLst = getMemberValue(md.info, t);

                    if (!md.listMd.isInited)
                    {
                        md.listMd.isInited = true;
                        if (lastLst != null)
                        {
                            md.listMd.funClear.Invoke(lastLst, new object[] { });
                        }
                        else
                        {
                            lastLst = Activator.CreateInstance(getMemberType(md.info));
                            setMemberValue(md.info, t, lastLst);
                        }
                    }

                    object obj = convertData(value, md.listMd.itemType);
                    //Debug.WriteLine("111:" + obj);
                    md.listMd.funAdd.Invoke(lastLst, new object[] { obj });
                } catch (Exception ex) {
                    Debug.WriteLine(ex.ToString());
                }
                return;
            }

            // bool type
            if (md.isBoolType)
            {
                setMemberValue(md.info, t, true);
                return;
            }

            // set value
            setData(t, md.info, value);
        }
Esempio n. 2
0
        private CmdAttrMd findMd(string key, ref int noKeyMdIdx)
        {
            CmdAttrMd md = null;

            if (mapName.ContainsKey(key))
            {
                return(mapName[key]);
            }
            if (noKeyMdIdx >= lstNoKeyMd.Count)
            {
                return(null);
            }

            md = lstNoKeyMd[noKeyMdIdx];
            ++noKeyMdIdx;
            return(md);
        }
Esempio n. 3
0
        private void parse(T t, string[] args)
        {
            CmdAttrMd lastMd     = null;
            int       noKeyMdIdx = 0;

            for (int i = 0; i < args.Length; ++i)
            {
                if (args[i] == "")
                {
                    continue;
                }

                KeyValueType argType = getKeyValue(args[i], out string key, out string value);

                if (argType != KeyValueType.Value)
                {
                    lastMd = findMd(key, ref noKeyMdIdx);
                }
                if (argType == KeyValueType.Value && lastMd == null)
                {
                    lastMd = findMd(key, ref noKeyMdIdx);
                }

                if (lastMd == null)
                {
                    continue;
                }

                if (argType == KeyValueType.Key && !lastMd.isBoolType)
                {
                    continue;
                }

                setData(lastMd, t, value);

                if (lastMd.listMd == null)
                {
                    lastMd = null;
                }
            }
        }
Esempio n. 4
0
        private void init()
        {
            //Regex regListItem = new Regex(@".*?\[\[(.*?),");
            Type type = typeof(T);

            var arrFields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            var arrProps  = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            MemberInfo[] arr = arrFields.Cast <MemberInfo>().Concat(arrProps).ToArray();

            if (type.IsDefined(typeof(CmdRoot), false))
            {
                CmdRoot cmdRoot = type.GetCustomAttribute <CmdRoot>();
                desc = cmdRoot.desc;
            }

            foreach (var mi in arr)
            {
                if (!mi.IsDefined(typeof(CmdAttr), false))
                {
                    continue;
                }

                Type miType = getMemberType(mi);

                var       attr = mi.GetCustomAttribute <CmdAttr>();
                CmdAttrMd md   = new CmdAttrMd(attr, mi);

                if (miType.IsGenericType)
                {
                    // type List<>
                    try {
                        md.listMd = new CmdAttrListMd();

                        Type[] arrItemType = miType.GetGenericArguments();
                        if (arrItemType.Length > 0)
                        {
                            md.listMd.itemType = arrItemType[0];
                        }
                        else
                        {
                        }
                        object obj = getMemberValue(mi, tMd);
                        if (obj == null)
                        {
                            md.listMd.funAdd   = miType.GetMethod("Add", arrItemType);
                            md.listMd.funClear = miType.GetMethod("Clear");
                        }
                        else
                        {
                            md.listMd.funAdd   = obj.GetType().GetMethod("Add", arrItemType);
                            md.listMd.funClear = obj.GetType().GetMethod("Clear");
                        }
                    } catch (Exception) { }
                }
                else
                {
                    md.defValue = getMemberValue(mi, tMd);
                }

                allKeyMd.Add(md);

                if (attr.name == "" && attr.shortName == "")
                {
                    lstNoKeyMd.Add(md);
                    continue;
                }

                if (miType == typeof(bool))
                {
                    md.isBoolType = true;
                }

                mapName[attr.name]      = md;
                mapName[attr.shortName] = md;
            }
        }