All pastes #972162 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-04-05 06:20 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;
			Arguments : list [NAssembly];
			ReturnType : System.Type;
			IL : list [NIL];
		}
		
		| Argument {
			ArgName : string;
			ArgType : System.Type;
		}
		
		| Other
	}
	
	public variant NIL {
		| Foo
	}
	
	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, 
								Arguments=ReflectMethodArguments(method),
								ReturnType=method.ReturnType,
								IL=ReflectMethodIL(method)
						);
					| _ => NAssembly.Other();
				};
				members = mem :: members;
			}
			
			members;
		}
		
		public ReflectMethodArguments(m : MethodInfo) : list [NAssembly] {
			mutable args : list [NAssembly] = [];
			
			foreach(param in m.GetParameters())
				args = NAssembly.Argument(param.Name, param.ParameterType) :: args;
			
			args;
		}
		
		public ReflectMethodIL(m : MethodInfo) : list [NIL] {
			mutable nil : list [NIL] = [];
			def body = m.GetMethodBody();
			def il = body.GetILAsByteArray();
			
			def Process(pos) {
			}
			
			Process(0);
			
			nil;
		}
	}
}