// // (c) 2009, Sigbjorn Finne // using System; using System.Reflection; using System.GACManagedAccess; namespace HsWrap { /// /// Given a type name, locate the metainfo needed to generate /// Haskell wrappers. /// public class TypeInfo { protected Type m_type; protected System.Reflection.MemberInfo[] m_members; static TypeInfo() { } public static Type GetType(System.String tyName) { try { Type t = Type.GetType(tyName); if (t != null) return t; } catch (Exception) { ; } System.GACManagedAccess.AssemblyCacheEnum enu = new System.GACManagedAccess.AssemblyCacheEnum(null); try { String asCand=enu.GetNextAssembly(); bool exhausted = false; bool checkTypes = false; while (!exhausted && asCand!=null) { try { Assembly tAss = Assembly.Load(asCand); AssemblyName aName = tAss.GetName(); String tySpec = String.Format("{0},{1}",tyName,aName.FullName); Type t = Type.GetType(tySpec); if ( t!=null ) { return t; } if ( checkTypes ) { foreach(Type ta in tAss.GetTypes()) { if ( ta.FullName.StartsWith(tyName) ) { Console.WriteLine("Found type matching prefix: {0}", ta.FullName); return ta; } } } Assembly localA = Assembly.Load(aName.FullName); t = localA.GetType(tyName); if ( t!=null ) {return t;} } catch (Exception) { //Console.WriteLine(e); } asCand=enu.GetNextAssembly(); if (asCand == null) { if ( checkTypes ) { exhausted = true; } else { Console.WriteLine("Warning: unable to find exact type match for {0}..", tyName); Console.WriteLine(" trying prefix-matching in case it is a generic class..."); checkTypes = true; enu = new System.GACManagedAccess.AssemblyCacheEnum(null); asCand=enu.GetNextAssembly(); } } } } catch(Exception e) { Console.WriteLine(e); } return null; } public static String GetSuperName(Type t, bool full) { if (t==null) { return (full ? "System.Object" : "Object"); } if (t.IsInterface ) { return (full ? "System.Object" : "Object"); } if (t.BaseType==null) { return (full ? t.FullName : t.Name); } if ( full ) { if ( t.BaseType.ContainsGenericParameters ) { return t.BaseType.ToString(); } return t.BaseType.FullName; } else { return t.BaseType.Name; } } public System.Type Type { get { return (m_type); } } public System.Reflection.MemberInfo[] Members { get { return (m_members); } } private bool myFilter(System.Reflection.MemberInfo m, System.Object filterCrit) { return( m.MemberType == System.Reflection.MemberTypes.Method || m.MemberType == System.Reflection.MemberTypes.Property || m.MemberType == System.Reflection.MemberTypes.Field); } public TypeInfo(System.String tyName) { m_type = TypeInfo.GetType(tyName); if ( m_type == null ) { return; } /* if ( m_type.IsGenericType ) { Type[] ts = m_type.GetGenericArguments(); Console.WriteLine("is generic {0}", ts.Length); for(int idx=0;idx