private IList <TypeRepTemplate> mkTemplates(string typeName) { List <TypeRepTemplate> rets = new List <TypeRepTemplate>(); Type t = assembly.GetType(typeName); if (t == null) { throw new Exception(String.Format("Type {0} not found", typeName)); } foreach (Type nestedTy in t.GetNestedTypes()) { foreach (TypeRepTemplate nestedRep in mkTemplates(nestedTy.FullName)) { rets.Add(nestedRep); } } TypeRepTemplate retRep = null; if (t.IsClass) { if (t.IsSubclassOf(typeof(System.Delegate))) { DelegateRepTemplate delRep = new DelegateRepTemplate(); buildDelegate(delRep, t); retRep = delRep; } else { ClassRepTemplate classRep = new ClassRepTemplate(); buildClass(classRep, t); retRep = classRep; } } else if (t.IsInterface) { InterfaceRepTemplate intRep = new InterfaceRepTemplate(); buildInterface(intRep, t); retRep = intRep; } else if (t.IsEnum) { EnumRepTemplate enumRep = new EnumRepTemplate(); enumRep.TypeName = TypeHelper.buildTypeName(t); foreach (FieldInfo f in t.GetFields(BindingFlags.Public | BindingFlags.Static)) { enumRep.Members.Add(new EnumMemberRepTemplate(f.Name, f.GetRawConstantValue().ToString())); } retRep = enumRep; } rets.Add(retRep); return(rets); }
private IList<TypeRepTemplate> mkTemplates(string typeName) { List<TypeRepTemplate> rets = new List<TypeRepTemplate>(); Type t = assembly.GetType(typeName); if (t == null) throw new Exception(String.Format("Type {0} not found", typeName)); foreach (Type nestedTy in t.GetNestedTypes()) { foreach (TypeRepTemplate nestedRep in mkTemplates(nestedTy.FullName)) { rets.Add(nestedRep); } } TypeRepTemplate retRep = null; if (t.IsClass) { if (t.IsSubclassOf(typeof(System.Delegate))) { DelegateRepTemplate delRep = new DelegateRepTemplate(); buildDelegate(delRep, t); retRep = delRep; } else { ClassRepTemplate classRep = new ClassRepTemplate(); buildClass(classRep, t); retRep = classRep; } } else if (t.IsInterface) { InterfaceRepTemplate intRep = new InterfaceRepTemplate(); buildInterface(intRep, t); retRep = intRep; } else if (t.IsEnum) { EnumRepTemplate enumRep = new EnumRepTemplate(); enumRep.TypeName = TypeHelper.buildTypeName(t); foreach (FieldInfo f in t.GetFields(BindingFlags.Public | BindingFlags.Static)) { enumRep.Members.Add(new EnumMemberRepTemplate(f.Name, f.GetRawConstantValue().ToString())); } retRep = enumRep; } rets.Add(retRep); return rets; }
private void buildInterface(InterfaceRepTemplate iface, Type t) { if (t.IsGenericType) { iface.TypeName = t.GetGenericTypeDefinition().FullName; string[] tParams = new string[t.GetGenericArguments().Length]; for (int i = 0; i < t.GetGenericArguments().Length; i++) { tParams[i] = t.GetGenericArguments()[i].Name; } iface.TypeParams = tParams; } else { iface.TypeName = t.FullName; } List<String> bases = new List<String>(); if (t.BaseType != null) bases.Add(TypeHelper.buildTypeName(t.BaseType)); foreach (Type iTy in t.GetInterfaces()) { bases.Add(TypeHelper.buildTypeName(iTy)); } iface.Inherits = bases.ToArray(); // Grab Methods foreach (MethodInfo m in t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)) { if (m.IsSpecialName) { // e.g., a property's getter / setter method continue; } MethodRepTemplate methRep = new MethodRepTemplate(); methRep.Name = m.Name; methRep.Return = new TypeRepRef(TypeHelper.buildTypeName(m.ReturnType)); if (m.IsGenericMethod) { string[] tParams = new string[m.GetGenericArguments().Length]; for (int i = 0; i < m.GetGenericArguments().Length; i++) { tParams[i] = m.GetGenericArguments()[i].Name; } methRep.TypeParams = tParams; } buildParameters(methRep.Params, m); if (m.IsStatic) { methRep.IsStatic = true; } methRep.SurroundingType = iface; iface.Methods.Add(methRep); } // Grab Properties foreach (PropertyInfo p in t.GetProperties()) { PropRepTemplate propRep = new PropRepTemplate(); propRep.Name = p.Name; propRep.Type = new TypeRepRef(TypeHelper.buildTypeName(p.PropertyType)); propRep.CanRead = p.CanRead; propRep.CanWrite = p.CanWrite; iface.Properties.Add(propRep); } // Grab Events foreach (EventInfo e in t.GetEvents()) { FieldRepTemplate eventRep = new FieldRepTemplate(); eventRep.Name = e.Name; eventRep.Type = new TypeRepRef(TypeHelper.buildTypeName(e.EventHandlerType)); iface.Events.Add(eventRep); } }
private void buildInterface(InterfaceRepTemplate iface, Type t) { if (t.IsGenericType) { iface.TypeName = t.GetGenericTypeDefinition().FullName; string[] tParams = new string[t.GetGenericArguments().Length]; for (int i = 0; i < t.GetGenericArguments().Length; i++) { tParams[i] = t.GetGenericArguments()[i].Name; } iface.TypeParams = tParams; } else { iface.TypeName = t.FullName; } List <String> bases = new List <String>(); if (t.BaseType != null) { bases.Add(TypeHelper.buildTypeName(t.BaseType)); } foreach (Type iTy in t.GetInterfaces()) { bases.Add(TypeHelper.buildTypeName(iTy)); } iface.Inherits = bases.ToArray(); // Grab Methods foreach (MethodInfo m in t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)) { if (m.IsSpecialName) { // e.g., a property's getter / setter method continue; } MethodRepTemplate methRep = new MethodRepTemplate(); methRep.Name = m.Name; methRep.Return = new TypeRepRef(TypeHelper.buildTypeName(m.ReturnType)); if (m.IsGenericMethod) { string[] tParams = new string[m.GetGenericArguments().Length]; for (int i = 0; i < m.GetGenericArguments().Length; i++) { tParams[i] = m.GetGenericArguments()[i].Name; } methRep.TypeParams = tParams; } buildParameters(methRep.Params, m); if (m.IsStatic) { methRep.IsStatic = true; } methRep.SurroundingType = iface; iface.Methods.Add(methRep); } // Grab Properties foreach (PropertyInfo p in t.GetProperties()) { PropRepTemplate propRep = new PropRepTemplate(); propRep.Name = p.Name; propRep.Type = new TypeRepRef(TypeHelper.buildTypeName(p.PropertyType)); propRep.CanRead = p.CanRead; propRep.CanWrite = p.CanWrite; iface.Properties.Add(propRep); } // Grab Events foreach (EventInfo e in t.GetEvents()) { FieldRepTemplate eventRep = new FieldRepTemplate(); eventRep.Name = e.Name; eventRep.Type = new TypeRepRef(TypeHelper.buildTypeName(e.EventHandlerType)); iface.Events.Add(eventRep); } }