コード例 #1
0
        public static bool Generate(Type structType, string key, out FieldReadWriteHelper readWrite)
        {
            var dict   = new Dictionary <FieldInfo, FieldReadWriteHelper>();
            var fields = structType.GetFields();

            try
            {
                foreach (var field in fields)
                {
                    if (field.IsStatic == false && PropertyReadWriteGenerator.GetOne(field.FieldType, key + "." + field.Name, out var fieldReadWrite))
                    {
                        dict[field] = fieldReadWrite;
                    }
                }

                var helper = new StructReadWriteHelper
                {
                    _readWrites = dict,
                    _type       = structType,
                };

                readWrite = helper;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                readWrite = null;
            }
            return(readWrite != null);
        }
コード例 #2
0
 private static void WriteObj(StructReadWriteHelper helper, object obj)
 {
     try
     {
         foreach (var pair in helper._readWrites)
         {
             pair.Value.Write(pair.Key.GetValue(obj));
         }
     }
     catch (Exception e)
     {
         Debug.LogException(e);
     }
 }
コード例 #3
0
        public static bool GetOne(Type pType, string key, out FieldReadWriteHelper propHelper)
        {
            if (HelperDefines.Contains(pType))
            {
                propHelper = FieldReadWriteHelper.Generate(key, HelperDefines.Get(pType));
            }
            else if (pType.IsValueType && StructReadWriteHelper.Generate(pType, key, out var readWrite))
            {
                propHelper = readWrite;
            }
            else
            {
                propHelper = null;
                throw new Exception("仅支持string, int, float, bool字段和由以上四种字段组成的结构体");
            }

            return(propHelper != null);
        }
コード例 #4
0
        private static object ReadObj(StructReadWriteHelper helper, object defaultValue)
        {
            var obj = Activator.CreateInstance(helper._type);

            try
            {
                foreach (var pair in helper._readWrites)
                {
                    pair.Key.SetValue(obj, pair.Value.Read(pair.Key.GetValue(defaultValue)));
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            return(obj);
        }