CSI.Interpreter.GetTypeName C# (CSharp) Method

GetTypeName() private method

private GetTypeName ( Type symType, bool useSimplifiedNamespaces ) : string
symType System.Type
useSimplifiedNamespaces bool
return string
        internal string GetTypeName(Type symType, bool useSimplifiedNamespaces)
        {
            if (symType == null)
            {
                return null;
            }
            if ((symType.IsGenericType) &&
                (symType.Namespace == "System") &&
                (symType.GetGenericArguments().Length == 1))
            {
                if (symType == typeof(bool?))
                {
                    return "bool?";
                }
                if (symType == typeof(sbyte?))
                {
                    return "sbyte?";
                }
                if (symType == typeof(byte?))
                {
                    return "byte?";
                }
                if (symType == typeof(char?))
                {
                    return "char?";
                }
                if (symType == typeof(short?))
                {
                    return "short?";
                }
                if (symType == typeof(ushort?))
                {
                    return "ushort?";
                }
                if (symType == typeof(int?))
                {
                    return "int?";
                }
                if (symType == typeof(uint?))
                {
                    return "uint?";
                }
                if (symType == typeof(long?))
                {
                    return "long?";
                }
                if (symType == typeof(ulong?))
                {
                    return "ulong?";
                }
                if (symType == typeof(float?))
                {
                    return "float?";
                }
                if (symType == typeof(double?))
                {
                    return "double?";
                }
                if (symType == typeof(decimal?))
                {
                    return "decimal?";
                }
                if ((symType == typeof(Guid?)) ||
                    (symType == typeof(DateTime?)))
                {
                    return GetTypeName(symType.GetGenericArguments()[0],
                        useSimplifiedNamespaces) + "?";
                }
            }
            string symTypeName;
            using (StringWriter stringWriter = new StringWriter())
            {
                gen.GenerateCodeFromExpression(new System.CodeDom.
                    CodeTypeReferenceExpression(symType), stringWriter, null);
                symTypeName = stringWriter.ToString();
            }
            if (useSimplifiedNamespaces)
            {
                foreach (string namespacePrefix in new string[]
                {
                    "System.Collections.Generic.",
                    "System.Collections.",
                    "System."
                })
                {
                    symTypeName = symTypeName.Replace(namespacePrefix, "");
                }
            }
            return symTypeName;
        }

Usage Example

        public static void Instantiate(Assembly a, Interpreter interp)
        {
            Hashtable table = interp.VarTable;

            try
            {
                CodeChunk chunk = (CodeChunk)a.CreateInstance("CsiChunk");
                chunk.Go(table);
                // we display the type and value of expressions.  The variable $_ is
                // always set, which is useful if you want to save the result of the last
                // calculation.
                if (interp.returnsValue && DumpingValue)
                {
                    string stype;
                    object val = table["_"];
                    if (val == null)
                    {
                        stype = null;
                    }
                    else
                    {
                        Type type = val.GetType();
                        stype = interp.GetPublicRuntimeTypeName(type, true);
                        stype = "(" + (stype ?? interp.GetTypeName(type, true)) + ")";
                    }
                    if (val == null)
                    {
                        Print("null");
                    }
                    else
                    if (val is string)
                    {
                        Print(stype, "'" + val + "'");
                    }
                    else
                    if (val is IEnumerable)
                    {
                        Print(stype);
                        Dumpl((IEnumerable)val);
                    }
                    else
                    {
                        Print(stype, val);
                    }
                }
            }
            catch (Exception ex)
            {
                Print(ex.GetType() + " was thrown: " + ex.Message);
            }
        }
All Usage Examples Of CSI.Interpreter::GetTypeName