All pastes #971503 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-04-04 20:01 UTC
rendered paste body
using System;
using System.Reflection;

namespace NPhilosopher.NILC {
	public variant NAssembly {
		| Assembly {
			AssemblyName : string;
			Types : list [NAssembly];
		}
		
		| Type {
			TypeName : string;
			Members : list [NAssembly];
		}
		
		| Method {
			MethodName : string;
			ReturnType : System.Type;
		}
		
		| Other
	}
	
	public module Reflector {
		public Reflect(file : string) : NAssembly {
			mutable types : list[NAssembly] = [];
			def assembly = Assembly.ReflectionOnlyLoadFrom(file);
			
			foreach(t in assembly.GetTypes())
				types = NAssembly.Type(t.FullName, ReflectMembers(t)) :: types;
			
			NAssembly.Assembly(assembly.FullName, types);
		}
		
		public ReflectMembers(t : Type) : list[NAssembly] {
			mutable members : list[NAssembly] = [];
			def flags = (
				BindingFlags.Instance |
				BindingFlags.Static |
				BindingFlags.Public |
				BindingFlags.NonPublic
			);
			
			foreach(member in t.GetMembers(flags)) {
				def mem = match(member.MemberType) {
					| MemberTypes.Method =>
						def method = member :> MethodInfo;
						NAssembly.Method(
								MethodName=member.Name, 
								ReturnType=method.ReturnType
						);
					| _ => NAssembly.Other();
				};
				members = mem :: members;
			}
			
			members;
		}
	}
}