Пример #1
0
        private void HandleToRam(string generator)
        {
            var dst1  = generator.Split(':');
            var name  = dst1.ElementAtOrDefault(0); //Name
            var value = dst1.ElementAtOrDefault(1); //Value
            var type  = dst1.ElementAtOrDefault(2); //Type

            //Remove if exsists the variable from ram
            if (Ram.Any((s) => s.Name == name))
            {
                Ram.Remove(Ram.First((s) => s.Name == name));
            }
            //The variable is not in ram and has to be created
            var actual_type = Type.GetType(type) ?? TypeAliasContainer.GetFromAlias(type);

            if (actual_type == null)
            {
                throw new InvalidVariableTypeException(string.Format("The type {0} is not a valid .Net or SelfLanguage type", type));
            }
            var convert = Conversion.GetConversion(actual_type);
            var obj     = GetVariableOfType(actual_type);

            if (convert.Any(s => s == PossibleConversion.IStringable))
            {
                dynamic o = obj;
                Ram.Add(new Variable(o.FromString(value), name));
            }
            else if (convert.Any(s => s == PossibleConversion.IConvertible))
            {
                var o = Convert.ChangeType(value, obj.GetType());
                Ram.Add(new Variable(o, name));
            }
            else if (convert.Any(s => s == PossibleConversion.Constructor))
            {
                var o = obj.GetType().GetConstructors().First((s) => s.GetParameters().Length == 1 && s.GetParameters().First().GetType() == typeof(string));
                Ram.Add(new Variable(o, name));
            }
            else if (convert.Any(s => s == PossibleConversion.FromStringImplicit))  //is the type implicitally convertible from string
            {
                obj = value;
                Ram.Add(new Variable(obj, name));
            }
            else if (convert.Any(s => s == PossibleConversion.FromStringExplicit))
            {
                obj = value; //TODO
            }
            else if (obj.GetType() is object)
            {
                obj = value;
                Ram.Add(new Variable(obj, name));
            }
            else
            {
                throw new InvalidVariableTypeException("The type {0} is not castable or convertible from string, and it does not have a constructor using one string argument");
            }
        }