예제 #1
0
        /// <summary>
        /// Get and resolve the .Net Type for the given type name.
        /// </summary>
        /// <param name="name">Type-name of the .Net type to be resolved. See remarks.</param>
        /// <remarks>
        /// The type-name can be one of:
        /// - C# alias, e.g. "string", "bool", "int", "float" etc.
        /// - IronSmalltak type prefixed with "_", e.g. "_SmalltalkRuntime".
        /// - mscorlib type (without any prefixes), e.g. "System.DateTime".
        /// - Any other type using assembly qualified name, e.g. "System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        /// </remarks>
        /// <returns>Returns System.Type with the given name or null of a type did not exist (could not be loaded).</returns>
        public static Type GetType(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (name == "bool")
            {
                return(typeof(bool));
            }
            if (name == "byte")
            {
                return(typeof(byte));
            }
            if (name == "sbyte")
            {
                return(typeof(sbyte));
            }
            if (name == "char")
            {
                return(typeof(char));
            }
            if (name == "decimal")
            {
                return(typeof(decimal));
            }
            if (name == "double")
            {
                return(typeof(double));
            }
            if (name == "float")
            {
                return(typeof(float));
            }
            if (name == "int")
            {
                return(typeof(int));
            }
            if (name == "uint")
            {
                return(typeof(uint));
            }
            if (name == "long")
            {
                return(typeof(long));
            }
            if (name == "ulong")
            {
                return(typeof(ulong));
            }
            if (name == "object")
            {
                return(typeof(object));
            }
            if (name == "short")
            {
                return(typeof(short));
            }
            if (name == "ushort")
            {
                return(typeof(ushort));
            }
            if (name == "string")
            {
                return(typeof(string));
            }
            if (name.StartsWith(NativeTypeClassMap.IstTypenamePrefix))
            {
                return(NativeTypeClassMap.GetIstType(name));
            }
            return(Type.GetType(name, false, false));
        }
예제 #2
0
        /// <summary>
        /// Register a .Net type identified by its type-name to be mapped to the given Smalltalk class.
        /// </summary>
        /// <param name="cls">Required. Smalltalk class to map to.</param>
        /// <param name="typeName">Required. Type-name of the .Net type to be mapped to Smalltalk class. See remarks.</param>
        /// <remarks>
        /// The type-name can be one of:
        /// - Reserved name: "true", "false", "null", "nil", "native", "class", "symbol", "pool", "type"
        /// - C# alias, e.g. "string", "bool", "int", "float" etc.
        /// - IronSmalltak type prefixed with "_", e.g. "_SmalltalkRuntime".
        /// - mscorlib type (without any prefixes), e.g. "System.DateTime".
        /// - Any other type using assembly qualified name, e.g. "System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        /// </remarks>
        public void RegisterClass(SmalltalkClass cls, string typeName)
        {
            if (cls == null)
            {
                throw new ArgumentNullException("cls");
            }
            if (System.String.IsNullOrWhiteSpace(typeName))
            {
                throw new SmalltalkDefinitionException(System.String.Format(
                                                           "Invalid type name. Check native type mapping for class {0}",
                                                           cls.Name), new ArgumentNullException("typeName"));
            }

            // Special case handling for Smalltalk specific classes.
            if (typeName == "true")
            {
                this.True = cls;
                return;
            }
            if (typeName == "false")
            {
                this.False = cls;
                return;
            }
            if ((typeName == "null") || (typeName == "nil"))
            {
                this.UndefinedObject = cls;
                return;
            }
            if (typeName == "native")
            {
                this.Native = cls;
                return;
            }
            if (typeName == "class")
            {
                this.Class = cls;
                return;
            }
            if (typeName == "symbol")
            {
                this.Symbol = cls;
                return;
            }
            if (typeName == "pool")
            {
                this.Pool = cls;
                return;
            }
            if (typeName == "type")
            {
                this.SystemType = cls;
                return;
            }
            //struct, enum, delegate, event ... do we need to handle those?

            // Generic classes ... remap C# convenience names to FCL names.
            Type type;

            if (typeName.StartsWith(NativeTypeClassMap.IstTypenamePrefix))
            {
                type = NativeTypeClassMap.GetIstType(typeName);
            }
            else
            {
                typeName = NativeTypeClassMap.RemapTypeName(typeName);
                try
                {
                    type = Type.GetType(typeName, true, false);
                }
                catch (Exception ex)
                {
                    throw new SmalltalkDefinitionException(System.String.Format(
                                                               "Cannot find type {0}. Check native type mapping for class {1}",
                                                               typeName, cls.Name), ex);
                }
            }

            // Some very common types are cached directly
            if (type == typeof(object))
            {
                this.Object = cls;
            }
            else if (type == typeof(int))
            {
                this.SmallInteger = cls;
            }
            else if (type == typeof(char))
            {
                this.Character = cls;
            }
            else if (type == typeof(string))
            {
                this.String = cls;
            }
            else if (type == typeof(double))
            {
                this.Float = cls;
            }
            else if (type == typeof(decimal))
            {
                this.SmallDecimal = cls;
            }
            else if (type == typeof(BigInteger))
            {
                this.BigInteger = cls;
            }
            else if (type == typeof(BigDecimal))
            {
                this.BigDecimal = cls;
            }

            // Add the type to the map ...
            this.TypeClassMap[type] = cls;
        }