Exemplo n.º 1
0
        public FldInfo ProbeAsField(string text)
        {
            string code = text.Replace(";", " ;");

            FldInfo retval = new FldInfo();

            if (code.IsSingleLine())
            {
                int indentLenth = code.Length - code.TrimStart().Length;
                retval.RootIndent = code.Substring(0, indentLenth);

                string[] tokens = ToSingleLine(code).Split(' '); //ToSingleLine also removes any extra spaces
                if (tokens.Last(0) == ";")
                {
                    // AccessModifiersAndType Name[=initializer];
                    retval.IsValid = true;
                    string[] declaration = tokens.TrimEnd(1).ToArray();

                    if (tokens.Any(x => x == "="))
                    {
                        declaration = tokens.TakeWhile(x => x != "=").ToArray();
                    }
                    retval.Name = declaration.Last();
                    retval.AccessModifiersAndType = string.Join(" ", declaration.TrimEnd(1)).TrimEnd();

                    var initExpression = tokens.Skip(declaration.Length);
                    if (initExpression.Any())
                    {
                        retval.Intitializer = string.Join(" ", initExpression.TrimEnd(1).ToArray());
                    }
                }
            }
            return(retval);
        }
Exemplo n.º 2
0
 private ClassInfo(Type t)
 {
     this.type = t;
     FieldInfo[] fis = t.GetFields(BindingFlags.Public | BindingFlags.Instance);
     for (int i = fis.Length; --i >= 0;)
     {
         FldInfo fi = new FldInfo(fis[i]);
         fields[fi.name] = fi;
     }
 }
Exemplo n.º 3
0
        public void ProbeAsFieldWithInitializer()
        {
            var     reflector = new CSharpRefactor();
            FldInfo info      = reflector.ProbeAsField("        int test = 0;");

            Assert.True(info.IsValid);
            Assert.AreEqual("test", info.Name);
            Assert.AreEqual("int", info.AccessModifiersAndType);
            Assert.AreEqual("= 0", info.Intitializer);

            var property = reflector.EmittFullProperty(info);
        }
Exemplo n.º 4
0
        public string EmittFullProperty(FldInfo fldInfo)
        {
            var propInfo = new PropInfo()
            {
                Name            = fldInfo.Name[0].ToString().ToUpper() + fldInfo.Name.Substring(1),
                AccessModifiers = "public " + fldInfo.AccessModifiers.Replace("private ", ""),
                RootIndent      = fldInfo.RootIndent,
                HasGetter       = true,
                HasSetter       = true
            };

            return(EmittFullProperty(propInfo));
        }
Exemplo n.º 5
0
        public FldInfo ProbeAsField(string text)
        {
            string code = text.Replace(";", " ;");

            FldInfo retval = new FldInfo();

            if (code.IsSingleLine())
            {
                int indentLenth = code.Length - code.TrimStart().Length;
                retval.RootIndent = code.Substring(0, indentLenth);

                string[] tokens = ToSingleLine(code).Split(' '); //ToSingleLine also removes any extra spaces
                if (tokens.Last(0) == ";")
                {
                    retval.IsValid         = true;
                    retval.Name            = tokens.Last(1);
                    retval.AccessModifiers = tokens.ConcatItems(0, tokens.Length - 1 - 2, " ").TrimEnd();
                }
            }
            return(retval);
        }
Exemplo n.º 6
0
    public static BonValue ToBon(object obj, HashSet <string> fields = null, Type declareType = null)
    {
        if (obj == null)
        {
            return(BonNull.value);
        }
        Type t = obj.GetType();

        switch (t.Name)
        {
        case "Byte": return((int)(byte)obj);

        case "SByte": return((int)(sbyte)obj);

        case "Int16": return((int)(short)obj);

        case "UInt16": return((int)(ushort)obj);

        case "Int32": return((int)obj);

        case "UInt32": return((int)(uint)obj);

        case "Int64": return((long)obj);

        case "UInt64": return((long)(ulong)obj);

        case "Single": return((float)obj);

        case "Double": return((double)obj);

        case "Boolean": return((bool)obj);

        case "String": return((string)obj);

        case "Byte[]": return((byte[])obj);

        default: {
            if (t.IsEnum)
            {
                return((int)obj);
            }
            break;
        }
        }

        switch (t.Name)
        {
        case "List`1": {
            Type     et  = t.GetGenericArguments()[0];
            BonArray arr = null;
            arr = new BonArray();
            IList list = (IList)obj;
            int   num  = list.Count;
            for (int i = 0; i < num; i++)
            {
                arr.Add(ToBon(list[i], fields, et));
            }
            return(arr);
        }

        case "Dictionary`2": {
            Type        et  = t.GetGenericArguments()[1];
            BonDocument doc = null;
            doc = new BonDocument();
            foreach (DictionaryEntry kv in (IDictionary)obj)
            {
                if (kv.Key.GetType().IsEnum)
                {
                    doc[((int)kv.Key).ToString()] = ToBon(kv.Value, fields, et);
                }
                else
                {
                    doc[kv.Key.ToString()] = ToBon(kv.Value, fields, et);
                }
            }
            return(doc);
        }

        default: {
            if (t.IsArray)
            {
                Type     et  = t.GetElementType();
                BonArray arr = null;
                arr = new BonArray();
                Array list = (Array)obj;
                int   num  = list.Length;
                for (int i = 0; i < num; i++)
                {
                    arr.Add(ToBon(list.GetValue(i), fields, et));
                }
                return(arr);
            }
            {
                if (obj is IBon)
                {
                    return(((IBon)obj).ToBon());
                }
                ClassInfo   ci  = ClassInfo.Get(t);
                BonDocument doc = new BonDocument();
                if (declareType != null && declareType != t)
                {
                    doc["_t_"] = t.FullName;
                }
                FldInfo[] fis = ci.fields.Values;
                for (int i = fis.Length; --i >= 0;)
                {
                    FldInfo fi = fis[i];
                    if (fields != null && !fields.Contains(fi.name))
                    {
                        continue;
                    }
                    doc[fi.name] = ToBon(fi.GetValue(obj), fi.subFields);
                }
                return(doc);
            }
        }
        }
    }