Exemplo n.º 1
0
        public object Create(Type targetType, object source)
        {
            StreamFactory.Refresh();
            if (source != null && Resources.Contains(source))
            {
                object?instance = Resources[source];
                if (targetType.IsInstanceOfType(instance))
                {
                    if (!InstanceCounts.ContainsKey(targetType))
                    {
                        InstanceCounts.Add(targetType, 1);
                    }
                    else
                    {
                        InstanceCounts[targetType]++;
                    }

                    return(instance);
                }
            }

            foreach (Type?type in FactoryFunctions.Keys)
            {
                if (type.IsAssignableFrom(targetType))
                {
#pragma warning disable CS8604 // Possible null reference argument.
                    object?instance = FactoryFunctions[type](targetType, source);
#pragma warning restore CS8604 // Possible null reference argument.
                    if (instance != null)
                    {
                        if (!InstanceCounts.ContainsKey(targetType))
                        {
                            InstanceCounts.Add(targetType, 1);
                        }
                        else
                        {
                            InstanceCounts[targetType]++;
                        }

                        if (source != null && (source is string) && IsResourceType(targetType) && !Resources.Contains(source.ToString()))
                        {
                            Resources.Add(source.ToString(), instance);
                        }
                        return(instance);
                    }
                }
            }
#pragma warning disable CS8603 // Possible null reference return.
            return(null);

#pragma warning restore CS8603 // Possible null reference return.
        }
Exemplo n.º 2
0
        public List <IElement> Analyze(string Input)
        {
            List <IElement>  elements  = new List <IElement>();
            FactoryOperators operators = new FactoryOperators();
            FactoryFunctions functions = new FactoryFunctions();
            int Count = 0;

            while (Input != "" & Count != 1000)
            {
                char first = char.Parse(Input.Substring(0, 1)); //first element from string
                if (char.IsDigit(first))                        // number
                {
                    double a = GetVal(Input, @"\-{0,1}\d{1,}[\.\,]{0,1}\d{0,}");
                    elements.Add(new ValueElement(a));
                    Input = Input.Remove(0, a.ToString().Length);
                }
                else if (first == 'x' | first == 'X') // variable
                {
                    elements.Add(new VariableElement());
                    Input = Input.Remove(0, 1);
                }
                else if (first == '(') // open bracket
                {
                    if (Input.Length == 1)
                    {
                        throw new Exception("Error in analyze. Lenght line cannot be 1");
                    }
                    KeyValuePair <string, double> tmp = TryToGetNegative(Input);
                    if (!double.IsNaN(tmp.Value))
                    {
                        elements.Add(new ValueElement(tmp.Value));
                        Input = Input.Remove(0, tmp.Key.Length);
                    }
                    else
                    {
                        elements.Add(new OpenBracket());
                        Input = Input.Remove(0, 1);
                    }
                }
                else if (first == ')') // close bracket
                {
                    elements.Add(new CloseBracket());
                    Input = Input.Remove(0, 1);
                }
                else if (char.IsLetter(first))
                {
                    string NameFunction = "";
                    string tmp          = "";
                    int    brackets     = 0;
                    do // get function name
                    {
                        NameFunction += tmp;
                        if (Input.Length == 0)
                        {
                            throw new Exception("Error in Analyze. Incoming string ended");
                        }
                        tmp   = Input.Substring(0, 1); //get first element
                        Input = Input.Remove(0, 1);    // remove first element from input
                    } while (tmp != "(");
                    brackets++;
                    string Context = "";
                    do // get context function
                    {
                        Context += tmp;
                        if (Input.Length == 0)
                        {
                            throw new Exception("Error in Analyze. Incoming string ended");
                        }
                        tmp   = Input.Substring(0, 1);
                        Input = Input.Remove(0, 1);
                        if (tmp == "(")
                        {
                            brackets++;
                        }
                        if (tmp == ")")
                        {
                            brackets--;
                        }
                    } while (tmp != ")" | brackets > 0);
                    Context = AlignBrackets(Context);
                    if (functions.Contains(NameFunction))
                    {
                        ClassFunction element = functions.Function;
                        element.Context = Analyze(Context);
                        elements.Add(element);
                    }
                    else
                    {
                        throw new Exception($"Error in Analyze. Unexpected value '{NameFunction}'");
                    }
                }
                else if (operators.Contains(first))
                {
                    elements.Add(operators.Operator);
                    Input = Input.Remove(0, operators.Operator.Name.Length);
                }
                else
                {
                    throw new Exception($"Error in Analyze. Unexpected item '{first.ToString()}'");
                }
                Count++;
            }

            return(elements);
        }