コード例 #1
0
        public HFTArgsBase(string prefix)
        {
            HFTLog        log     = new HFTLog("HFTArgs");
            HFTArgChecker checker = new HFTArgChecker(log, prefix);

            HFTArgParser p = HFTArgParser.GetInstance();

            System.Reflection.FieldInfo[] fields = this.GetType().GetFields();
            foreach (System.Reflection.FieldInfo info in fields)
            {
                object field    = System.Activator.CreateInstance(info.FieldType);
                string dashName = checker.GetHFTDashName(info.Name);
                checker.AddArg(dashName);
                info.FieldType.GetMethod("Init").Invoke(field, new object[] { dashName, p });
                info.SetValue(this, field);
            }

            checker.CheckArgs();
        }
コード例 #2
0
        public static bool Apply(string prefix, object obj)
        {
            HFTLog        log     = new HFTLog("HFTArgsDirect");
            HFTArgChecker checker = new HFTArgChecker(log, prefix);

            HFTArgParser p = HFTArgParser.GetInstance();

            System.Reflection.FieldInfo[] fields = obj.GetType().GetFields();
            foreach (System.Reflection.FieldInfo info in fields)
            {
                //object field = System.Activator.CreateInstance(info.FieldType);
                object      value     = null;
                System.Type fieldType = info.FieldType;
                string      dashName  = checker.GetHFTDashName(info.Name);
                checker.AddArg(dashName);
                // Should do this with reflection but ...
                if (fieldType == typeof(string))
                {
                    string strValue = "";
                    if (p.TryGet <string>(dashName, ref strValue))
                    {
                        value = strValue;
                    }
                }
                else if (fieldType == typeof(int))
                {
                    int intValue = 0;
                    if (p.TryGet <int>(dashName, ref intValue))
                    {
                        value = intValue;
                    }
                }
                else if (fieldType == typeof(bool))
                {
                    bool boolValue = false;
                    if (p.TryGetBool(dashName, ref boolValue))
                    {
                        value = boolValue;
                    }
                }
                else if (fieldType == typeof(float))
                {
                    float floatValue = 0;
                    if (p.TryGet <float>(dashName, ref floatValue))
                    {
                        value = floatValue;
                    }
                }
                else
                {
                    throw new System.InvalidOperationException("no support for type: " + fieldType.Name);
                }

                if (value != null)
                {
                    info.SetValue(obj, value);
                }
            }

            return(checker.CheckArgs());
        }