コード例 #1
0
ファイル: NodeManager.cs プロジェクト: zaguarman/SUCC
        internal static object GetNodeData(Node node, Type type)
        {
            System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);

            try
            {
                if (type == typeof(string) && node.Value == MultiLineStringNode.Terminator && node.ChildLines.Count > 0)
                {
                    return(BaseTypes.ParseSpecialStringCase(node));
                }

                if (BaseTypes.IsBaseType(type))
                {
                    return(BaseTypes.ParseBaseType(node.Value, type));
                }

                var collection = CollectionTypes.TryGetCollection(node, type);
                if (collection != null)
                {
                    return(collection);
                }

                if (!String.IsNullOrEmpty(node.Value))
                {
                    return(ComplexTypeShortcuts.GetFromShortcut(node.Value, type));
                }

                return(ComplexTypes.RetrieveComplexType(node, type));
            }
            catch (Exception e)
            {
                throw new Exception($"Error getting data of type {type} from node: {e.InnerException}");
            }
        }
コード例 #2
0
        private static Dictionary <TKey, TValue> RetrieveDictionaryGeneric <TKey, TValue>(Node node)
        {
            bool keyIsBase = BaseTypes.IsBaseType(typeof(TKey));

            var dictionary = new Dictionary <TKey, TValue>(capacity: node.ChildNodes.Count);

            if (keyIsBase && node.ChildNodeType == NodeChildrenType.key)
            {
                foreach (var child in node.ChildNodes)
                {
                    string childKey = (child as KeyNode).Key;
                    var    key      = BaseTypes.ParseBaseType <TKey>(childKey);
                    var    value    = NodeManager.GetNodeData <TValue>(child);
                    dictionary.Add(key, value);
                }
            }
            else
            {
                var array = NodeManager.GetNodeData <WritableKeyValuePair <TKey, TValue>[]>(node);
                foreach (var kvp in array)
                {
                    dictionary.Add(kvp.key, kvp.value);
                }
            }

            return(dictionary);
        }
コード例 #3
0
ファイル: ComplexTypeShortcuts.cs プロジェクト: pipe01/SUCC
        private static bool TryConstructorShortcut(string shortcut, Type type, out object result)
        {
            try {
                if (shortcut.StartsWith("(") && shortcut.EndsWith(")"))
                {
                    string text         = shortcut.Substring(1, shortcut.Length - 2); // remove the ( and )
                    var    paramStrings = text.Split(',');

                    var constructors = type.GetConstructors();

                    ConstructorInfo constructor = constructors[0];

                    if (constructors.Length > 1)
                    {
                        foreach (var c in constructors)
                        {
                            // todo: it would be nice to check constructor parameter types for compatibility with paramStrings.
                            // say a type had one constructor that took a string, and one constructor that took an int. We should be able to pick the right one.
                            if (c.GetParameters().Length == paramStrings.Length)
                            {
                                constructor = c;
                                break;
                            }
                        }
                    }

                    var parameters        = constructor.GetParameters();
                    var constructorParams = new object[parameters.Length];
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        if (i < paramStrings.Length)
                        {
                            constructorParams[i] = BaseTypes.ParseBaseType(paramStrings[i].Trim(), parameters[i].ParameterType);
                        }
                        else // optional parameter support
                        {
                            constructorParams[i] = parameters[i].DefaultValue;
                        }
                    }

                    result = constructor.Invoke(constructorParams);
                    return(true);
                }
            } catch { }   // I am a good programmer

            result = null;
            return(false);
        }
コード例 #4
0
ファイル: ReadableDataFile.cs プロジェクト: pipe01/SUCC
        /// <summary> Interpret this file as a dictionary. Top-level keys in the file are interpreted as keys in the dictionary. </summary>
        /// <remarks> TKey must be a Base Type </remarks>
        public Dictionary <TKey, TValue> GetAsDictionary <TKey, TValue>()
        {
            if (!BaseTypes.IsBaseType(typeof(TKey)))
            {
                throw new Exception("When using GetAsDictionary, TKey must be a base type");
            }

            var keys       = this.TopLevelKeys;
            var dictionary = new Dictionary <TKey, TValue>(capacity: keys.Count);

            foreach (var keyText in keys)
            {
                TKey   key   = BaseTypes.ParseBaseType <TKey>(keyText);
                TValue value = NodeManager.GetNodeData <TValue>(TopLevelNodes[keyText]);
                dictionary.Add(key, value);
            }

            return(dictionary);
        }
コード例 #5
0
ファイル: ComplexTypeShortcuts.cs プロジェクト: pipe01/SUCC
        private static bool TryMethodShortcut(string shortcut, Type type, out object result)
        {
            try {
                if (shortcut.Contains("(") && shortcut.Contains(")"))
                {
                    string methodname = shortcut.Substring(0, shortcut.IndexOf('('));
                    var    method     = type.GetMethod(methodname, BindingFlags.Public | BindingFlags.Static);

                    if (method != null && method.ReturnType == type)
                    {
                        var parameters = method.GetParameters();

                        string s            = shortcut.Substring(shortcut.IndexOf('(') + 1, shortcut.Length - shortcut.IndexOf('(') - 2);
                        var    paramStrings = s.Split(',');

                        var methodParams = new object[parameters.Length];
                        for (int i = 0; i < parameters.Length; i++)
                        {
                            if (i < paramStrings.Length)
                            {
                                methodParams[i] = BaseTypes.ParseBaseType(paramStrings[i].Trim(), parameters[i].ParameterType);
                            }
                            else // optional parameter support
                            {
                                methodParams[i] = parameters[i].DefaultValue;
                            }
                        }

                        result = method.Invoke(null, methodParams);
                        return(true);
                    }
                }
            } catch { }  // Who am I kidding. I am a bad programmer :(

            result = null;
            return(false);
        }