public static dynamic GenerateModel(Assembly assembly, string winRTNamespace) { if (!VerifyNamespaceInAssembly(assembly, winRTNamespace)) { throw new Exception(String.Format("The namespace {0} is not defined in the given WinMD file.", winRTNamespace)); } dynamic mainModel = new DynamicDictionary(); TX.MainModel = mainModel; mainModel.Assembly = assembly; var types = new Dictionary <Type, dynamic>(); mainModel.Types = types; var namespaces = new List <String>(); namespaces.Add("NodeRT"); namespaces.AddRange(winRTNamespace.Split('.')); mainModel.Namespaces = namespaces; mainModel.WinRTNamespace = winRTNamespace; var filteredTypes = GetTypesForNamespace(assembly, winRTNamespace); mainModel.Enums = (from t in assembly.ExportedTypes where t.Namespace.Equals(winRTNamespace, StringComparison.InvariantCultureIgnoreCase) && t.IsEnum select t).ToArray(); mainModel.ValueTypes = (from t in assembly.ExportedTypes where t.Namespace.Equals(winRTNamespace, StringComparison.InvariantCultureIgnoreCase) && t.IsValueType && !t.IsEnum select t).ToArray(); // use this container to aggregate value types which are not in the namespace // we will need to generate converter methods for those types mainModel.ExternalReferencedValueTypes = new List <Type>(); // use this container to aggreate other namesapces which are references from this namespaces // we will need to create a dependancy list from these namespaces mainModel.ExternalReferencedNamespaces = new List <String>(); foreach (var t in filteredTypes) { dynamic typeDefinition = new DynamicDictionary(); types.Add(t, typeDefinition); typeDefinition.Name = t.Name; typeDefinition.MemberProperties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); typeDefinition.StaticProperties = t.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); var memberEvents = t.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy).Select(eventInfo => { dynamic dict = new DynamicDictionary(); dict.EventInfo = eventInfo; dict.IsStatic = false; return(dict); }).ToArray(); var staticEvents = t.GetEvents(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Select(eventInfo => { dynamic dict = new DynamicDictionary(); dict.EventInfo = eventInfo; dict.IsStatic = true; return(dict); }).ToArray(); // one function will handle both static and member events, so put them in the same list typeDefinition.Events = memberEvents.Concat(staticEvents).ToArray(); // this will be used in the InitExports method typeDefinition.HasStaticEvents = (staticEvents.Length > 0); typeDefinition.HasMemberEvents = (memberEvents.Length > 0); typeDefinition.Type = t; var publicMethods = t.GetRuntimeMethods().Where((methodInfo) => { return(methodInfo.DeclaringType == t && (methodInfo.IsPublic || methodInfo.IsHideBySig) && !methodInfo.Name.StartsWith("add_") && !methodInfo.Name.StartsWith("remove_") && !methodInfo.Name.StartsWith("get_") && !methodInfo.Name.StartsWith("put_") && !methodInfo.IsStatic && !TX.ShouldIgnoreMethod(methodInfo)); }).GroupBy(methodInfo => methodInfo.Name).Select(method => { dynamic dict = new DynamicDictionary(); dict.Name = method.Key; dict.Overloads = method.ToArray(); return(dict); }).ToArray(); typeDefinition.MemberAsyncMethods = publicMethods.Where((methodInfo) => { return(TX.IsAsync(methodInfo.Overloads[0])); }).ToArray(); typeDefinition.MemberSyncMethods = publicMethods.Where((methodInfo) => { return(!TX.IsAsync(methodInfo.Overloads[0])); }).ToArray(); var staticMethods = t.GetRuntimeMethods().Where((methodInfo) => { return(methodInfo.DeclaringType == t && (methodInfo.IsPublic || methodInfo.IsHideBySig) && !methodInfo.Name.StartsWith("add_") && !methodInfo.Name.StartsWith("remove_") && !methodInfo.Name.StartsWith("get_") && !methodInfo.Name.StartsWith("put_") && methodInfo.IsStatic && !TX.ShouldIgnoreMethod(methodInfo)); }).GroupBy(methodInfo => methodInfo.Name).Select(method => { dynamic dict = new DynamicDictionary(); dict.Name = method.Key; dict.Overloads = method.ToArray(); return(dict); }).ToArray(); typeDefinition.StaticAsyncMethods = staticMethods.Where((methodInfo) => { return(TX.IsAsync(methodInfo.Overloads[0])); }).ToArray(); typeDefinition.StaticSyncMethods = staticMethods.Where((methodInfo) => { return(!TX.IsAsync(methodInfo.Overloads[0])); }).ToArray(); ExternalTypesHelper.GetExternalReferencedDataForType(typeDefinition, mainModel.ExternalReferencedValueTypes, mainModel.ExternalReferencedNamespaces); } return(mainModel); }
public static dynamic GenerateModel(Assembly assembly, string winRTNamespace) { if (!VerifyNamespaceInAssembly(assembly, winRTNamespace)) { throw new Exception(String.Format("The namespace {0} is not defined in the given WinMD file.", winRTNamespace)); } dynamic mainModel = new DynamicDictionary(); TX.MainModel = mainModel; mainModel.Assembly = assembly; var types = new Dictionary<Type, dynamic>(); mainModel.Types = types; var namespaces = new List<String>(); namespaces.Add("NodeRT"); namespaces.AddRange(winRTNamespace.Split('.')); mainModel.Namespaces = namespaces; mainModel.WinRTNamespace = winRTNamespace; var filteredTypes = GetTypesForNamespace(assembly, winRTNamespace); mainModel.Enums = (from t in assembly.ExportedTypes where t.Namespace.Equals(winRTNamespace, StringComparison.InvariantCultureIgnoreCase) && t.IsEnum select t).ToArray(); mainModel.ValueTypes = (from t in assembly.ExportedTypes where t.Namespace.Equals(winRTNamespace, StringComparison.InvariantCultureIgnoreCase) && t.IsValueType && !t.IsEnum select t).ToArray(); // use this container to aggregate value types which are not in the namespace // we will need to generate converter methods for those types mainModel.ExternalReferencedValueTypes = new List<Type>(); // use this container to aggreate other namesapces which are references from this namespaces // we will need to create a dependancy list from these namespaces mainModel.ExternalReferencedNamespaces = new List<String>(); foreach (var t in filteredTypes) { dynamic typeDefinition = new DynamicDictionary(); types.Add(t, typeDefinition); typeDefinition.Name = t.Name; typeDefinition.MemberProperties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); typeDefinition.StaticProperties = t.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); var memberEvents = t.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy).Select(eventInfo => { dynamic dict = new DynamicDictionary(); dict.EventInfo = eventInfo; dict.IsStatic = false; return dict; }).ToArray(); var staticEvents = t.GetEvents(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Select(eventInfo => { dynamic dict = new DynamicDictionary(); dict.EventInfo = eventInfo; dict.IsStatic = true; return dict; }).ToArray(); // one function will handle both static and member events, so put them in the same list typeDefinition.Events = memberEvents.Concat(staticEvents).ToArray(); // this will be used in the InitExports method typeDefinition.HasStaticEvents = (staticEvents.Length > 0); typeDefinition.HasMemberEvents = (memberEvents.Length > 0); typeDefinition.Type = t; var publicMethods = t.GetRuntimeMethods().Where((methodInfo) => { return methodInfo.DeclaringType == t && (methodInfo.IsPublic || methodInfo.IsHideBySig) && !methodInfo.Name.StartsWith("add_") && !methodInfo.Name.StartsWith("remove_") && !methodInfo.Name.StartsWith("get_") && !methodInfo.Name.StartsWith("put_") && !methodInfo.IsStatic && !TX.ShouldIgnoreMethod(methodInfo); }).GroupBy(methodInfo => methodInfo.Name).Select(method => { dynamic dict = new DynamicDictionary(); dict.Name = method.Key; dict.Overloads = method.ToArray(); return dict; }).ToArray(); typeDefinition.MemberAsyncMethods = publicMethods.Where((methodInfo) => { return TX.IsAsync(methodInfo.Overloads[0]); }).ToArray(); typeDefinition.MemberSyncMethods = publicMethods.Where((methodInfo) => { return !TX.IsAsync(methodInfo.Overloads[0]); }).ToArray(); var staticMethods = t.GetRuntimeMethods().Where((methodInfo) => { return methodInfo.DeclaringType == t && (methodInfo.IsPublic || methodInfo.IsHideBySig) && !methodInfo.Name.StartsWith("add_") && !methodInfo.Name.StartsWith("remove_") && !methodInfo.Name.StartsWith("get_") && !methodInfo.Name.StartsWith("put_") && methodInfo.IsStatic && !TX.ShouldIgnoreMethod(methodInfo); }).GroupBy(methodInfo => methodInfo.Name).Select(method => { dynamic dict = new DynamicDictionary(); dict.Name = method.Key; dict.Overloads = method.ToArray(); return dict; }).ToArray(); typeDefinition.StaticAsyncMethods = staticMethods.Where((methodInfo) => { return TX.IsAsync(methodInfo.Overloads[0]); }).ToArray(); typeDefinition.StaticSyncMethods = staticMethods.Where((methodInfo) => { return !TX.IsAsync(methodInfo.Overloads[0]); }).ToArray(); ExternalTypesHelper.GetExternalReferencedDataForType(typeDefinition, mainModel.ExternalReferencedValueTypes, mainModel.ExternalReferencedNamespaces); } return mainModel; }