Esempio n. 1
0
        public Signature GetNextSignature(ref int pos)
        {
            if (data == null)
            {
                return(Signature.Empty);
            }

            DType dtype = (DType)data[pos++];

            switch (dtype)
            {
            //case DType.Invalid:
            //	return typeof (void);
            case DType.Array:
                //peek to see if this is in fact a dictionary
                if ((DType)data[pos] == DType.DictEntryBegin)
                {
                    //skip over the {
                    pos++;
                    Signature keyType   = GetNextSignature(ref pos);
                    Signature valueType = GetNextSignature(ref pos);
                    //skip over the }
                    pos++;
                    return(Signature.MakeDict(keyType, valueType));
                }
                else
                {
                    Signature elementType = GetNextSignature(ref pos);
                    return(elementType.MakeArraySignature());
                }

            //case DType.DictEntryBegin: // FIXME: DictEntries should be handled separately.
            case DType.StructBegin:
                //List<Signature> fieldTypes = new List<Signature> ();
                Signature fieldsSig = Signature.Empty;
                while ((DType)data[pos] != DType.StructEnd)
                {
                    fieldsSig += GetNextSignature(ref pos);
                }
                //skip over the )
                pos++;
                return(Signature.MakeStruct(fieldsSig));

            //return fieldsSig;
            case DType.DictEntryBegin:
                Signature sigKey   = GetNextSignature(ref pos);
                Signature sigValue = GetNextSignature(ref pos);
                //skip over the }
                pos++;
                return(Signature.MakeDictEntry(sigKey, sigValue));

            default:
                return(new Signature(dtype));
            }
        }
        public static Signature GetSig(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            //this is inelegant, but works for now
            if (type == typeof(Signature))
            {
                return(new Signature(DType.Signature));
            }

            if (type == typeof(ObjectPath))
            {
                return(new Signature(DType.ObjectPath));
            }

            if (type == typeof(void))
            {
                return(Signature.Empty);
            }

            if (type == typeof(string))
            {
                return(new Signature(DType.String));
            }

            if (type == typeof(object))
            {
                return(new Signature(DType.Variant));
            }

            if (type.IsArray)
            {
                return(GetSig(type.GetElementType()).MakeArraySignature());
            }

            if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IDictionary <,>) || type.GetGenericTypeDefinition() == typeof(Dictionary <,>)))
            {
                Type[] genArgs = type.GetGenericArguments();
                return(Signature.MakeDict(GetSig(genArgs[0]), GetSig(genArgs[1])));
            }

            if (Mapper.IsPublic(type))
            {
                return(new Signature(DType.ObjectPath));
            }

            if (!type.IsPrimitive && !type.IsEnum)
            {
                Signature sig = Signature.Empty;

                foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    sig += GetSig(fi.FieldType);
                }

                return(Signature.MakeStruct(sig));
            }

            DType dtype = Signature.TypeToDType(type);

            return(new Signature(dtype));
        }