public static TypeReference GetTypeReference(string namespaceURI, string typename, ModuleDefinition module, IXmlLineInfo xmlInfo, ModeOfGetType info) { return(new XmlType(namespaceURI, typename, null).GetTypeReference(info, module, xmlInfo)); }
public static TypeReference GetTypeReference(this XmlType xmlType, ModeOfGetType mode, ModuleDefinition module, IXmlLineInfo xmlInfo, bool fromAllAssembly = false) { if (s_xmlnsDefinitions == null) { GatherXmlnsDefinitionAttributes(); } var namespaceURI = xmlType.NamespaceUri; var elementName = xmlType.Name; var typeArguments = xmlType.TypeArguments; if (elementName.Contains("-")) { elementName = elementName.Replace('-', '+'); } var lookupAssemblies = new List <XmlnsDefinitionAttribute>(); var lookupNames = new List <string>(); if (true == fromAllAssembly) { foreach (var xmlnsDef in s_xmlnsDefinitions) { lookupAssemblies.Add(xmlnsDef); } } else { foreach (var xmlnsDef in s_xmlnsDefinitions) { if (xmlnsDef.XmlNamespace != namespaceURI) { continue; } lookupAssemblies.Add(xmlnsDef); } } if (lookupAssemblies.Count == 0) { string ns; string typename; string asmstring; string targetPlatform; XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out targetPlatform); asmstring = asmstring ?? module.Assembly.Name.Name; if (ns != null) { lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns, 0) { AssemblyName = asmstring }); } } switch (mode) { case ModeOfGetType.OnlyGetType: lookupNames.Add(elementName); break; case ModeOfGetType.OnlyGetTypeExtension: lookupNames.Add(elementName + "Extension"); break; case ModeOfGetType.Both: lookupNames.Add(elementName); lookupNames.Add(elementName + "Extension"); break; } for (var i = 0; i < lookupNames.Count; i++) { var name = lookupNames[i]; if (name.Contains(":")) { name = name.Substring(name.LastIndexOf(':') + 1); } if (typeArguments != null) { name += "`" + typeArguments.Count; //this will return an open generic Type } lookupNames[i] = name; } TypeReference type = null; foreach (var asm in lookupAssemblies) { if (type != null) { break; } foreach (var name in lookupNames) { if (type != null) { break; } var clrNamespace = asm.ClrNamespace; var typeName = name.Replace('+', '/'); //Nested types var idx = typeName.LastIndexOf('.'); if (idx >= 0) { clrNamespace += '.' + typeName.Substring(0, typeName.LastIndexOf('.')); typeName = typeName.Substring(typeName.LastIndexOf('.') + 1); } type = module.GetTypeDefinition((asm.AssemblyName, clrNamespace, typeName)); } } if (type != null && typeArguments != null && type.HasGenericParameters) { type = module.ImportReference(type) .MakeGenericInstanceType(typeArguments.Select(x => GetTypeReference(x, mode, module, xmlInfo)).ToArray()); } if (type == null) { throw new XamlParseException(string.Format("Type {0} not found in xmlns {1}", elementName, namespaceURI), xmlInfo); } return(module.ImportReference(type)); }
public static TypeReference GetTypeReference(string xmlType, ModuleDefinition module, BaseNode node, ModeOfGetType mode) { var split = xmlType.Split(':'); if (split.Length > 2) { throw new XamlParseException($"Type \"{xmlType}\" is invalid", node as IXmlLineInfo); } string prefix, name; if (split.Length == 2) { prefix = split[0]; name = split[1]; } else { prefix = ""; name = split[0]; } var namespaceuri = node.NamespaceResolver.LookupNamespace(prefix) ?? ""; return(GetTypeReference(new XmlType(namespaceuri, name, null), mode, module, node as IXmlLineInfo)); }