/* * Copyright (c), 2009 Sigbjorn Finne * */ using System; using System.Collections; using System.Runtime.InteropServices; namespace HsInvoker { [ComVisible(false)] public enum DotnetType { Dotnet_Byte = 0, Dotnet_Boolean, Dotnet_Char, Dotnet_Double, Dotnet_Float, Dotnet_Int, Dotnet_Int8, Dotnet_Int16, Dotnet_Int32, Dotnet_Int64, Dotnet_Word8, Dotnet_Word16, Dotnet_Word32, Dotnet_Word64, Dotnet_Ptr, Dotnet_Unit, Dotnet_Object, Dotnet_String }; // // Class: TypeName // // Purpose: pairing up an assembly name and the type/class name. // [ComVisible(false)] public class TypeName { public String m_assembly; public String m_class; public int m_length; public TypeName() { m_assembly = String.Empty; m_class = String.Empty; m_length = 0; } public void Print() { if ( m_assembly!=null && m_assembly != String.Empty ) { Console.Write("["); Console.Write(m_assembly); Console.Write("]"); } Console.WriteLine(m_class); } public int Length() { return m_length; } public String toStdString() { String res = new String(m_class.ToCharArray()); if ( m_assembly!=null && m_assembly != String.Empty ) { res = String.Concat(res, ","); res = String.Concat(res, m_assembly); } return res; } public static TypeName ParseType(String str) { int curPos = 0; int endPos; //Console.WriteLine("x{0}y", str); TypeName typeName = new TypeName(); if ( str.Length == 0 ) { typeName.m_assembly = ""; typeName.m_length = 0; typeName.m_class = ""; return typeName; } if ( str[0] == '[' ) { endPos = str.IndexOf(']'); curPos = endPos + 1; typeName.m_assembly = str.Substring(1,endPos-1); typeName.m_length = endPos+1; } Char[] delims = {' ',',','(',')'}; endPos = str.IndexOfAny(delims,curPos); if ( endPos == -1 ) { typeName.m_class = str.Substring(curPos); } else { typeName.m_class = str.Substring(curPos,endPos-curPos); } // typeName.m_class = str.Substring(curPos,endPos-curPos); typeName.m_length += endPos-curPos; return typeName; } public static void ParseTypeArgs(String typeName, int idx, ref ArrayList tyArgs) { int i = idx; while (i < typeName.Length) { if ( typeName[i] == '[' ) { int start = i+1; int nest = 1; i++; while ( i < typeName.Length ) { if ( typeName[i] == '[' ) { nest++; } else if ( typeName[i] == ']' ) { if ( nest == 1 ) { tyArgs.Add(typeName.Substring(start,i-start)); break; } nest--; } i++; } } i++; } } public static Type GetDotnetType(int dty) { switch ((DotnetType)dty) { case DotnetType.Dotnet_Byte: return typeof(System.Byte); case DotnetType.Dotnet_Boolean: return typeof(System.Boolean); case DotnetType.Dotnet_Char: return typeof(System.Char); case DotnetType.Dotnet_Double: return typeof(System.Double); case DotnetType.Dotnet_Float: return typeof(System.Single); case DotnetType.Dotnet_Int: return typeof(System.Int32); case DotnetType.Dotnet_Int8: return typeof(System.SByte); case DotnetType.Dotnet_Int16: return typeof(System.Int16); case DotnetType.Dotnet_Int32: return typeof(System.Int32); case DotnetType.Dotnet_Int64: return typeof(System.Int64); case DotnetType.Dotnet_Word8: return typeof(System.Byte); case DotnetType.Dotnet_Word16: return typeof(System.UInt16); case DotnetType.Dotnet_Word32: return typeof(System.UInt32); case DotnetType.Dotnet_Word64: return typeof(System.UInt64); case DotnetType.Dotnet_Ptr: return typeof(System.IntPtr); case DotnetType.Dotnet_Unit: return typeof(void); case DotnetType.Dotnet_String: return typeof(System.String); case DotnetType.Dotnet_Object: return typeof(System.Object); default: return typeof(System.Object); } } }; }