// // Lex the next segment as an assembly name embedded inside a generic argument type. // // Terminated by an unescaped ']'. // public RuntimeAssemblyName GetNextEmbeddedAssemblyName() { SkipWhiteSpace(); int src = _index; char[] buffer = new char[_chars.Length]; int dst = 0; for (; ;) { char c = _chars[src]; if (c == NUL) { throw new ArgumentException(); } if (c == ']') { break; } src++; // Backslash can be used to escape a ']' - any other backslash character is left alone (along with the backslash) // for the AssemblyName parser to handle. if (c == '\\' && _chars[src] == ']') { c = _chars[src++]; } buffer[dst++] = c; } _index = src; String fullName = new String(buffer, 0, dst); return(AssemblyNameParser.Parse(fullName)); }
public NativeFormatModule GetModuleFromAssemblyName(string assemblyNameString) { AssemblyBindResult bindResult; RuntimeAssemblyName assemblyName = AssemblyNameParser.Parse(assemblyNameString); Exception failureException; if (!AssemblyBinderImplementation.Instance.Bind(assemblyName, cacheMissedLookups: true, out bindResult, out failureException)) { throw failureException; } var moduleList = Internal.Runtime.TypeLoader.ModuleList.Instance; NativeFormatModuleInfo primaryModule = moduleList.GetModuleInfoForMetadataReader(bindResult.Reader); // If this isn't the primary module, defer to that module to load the MetadataUnit if (primaryModule != _module) { return(Context.ResolveMetadataUnit(primaryModule).GetModule(bindResult.ScopeDefinitionHandle)); } // Setup arguments and allocate the NativeFormatModule ArrayBuilder <NativeFormatModule.QualifiedScopeDefinition> qualifiedScopes = new ArrayBuilder <NativeFormatModule.QualifiedScopeDefinition>(); qualifiedScopes.Add(new NativeFormatModule.QualifiedScopeDefinition(this, bindResult.ScopeDefinitionHandle)); foreach (QScopeDefinition scope in bindResult.OverflowScopes) { NativeFormatModuleInfo module = moduleList.GetModuleInfoForMetadataReader(scope.Reader); ScopeDefinitionHandle scopeHandle = scope.Handle; NativeFormatMetadataUnit metadataUnit = Context.ResolveMetadataUnit(module); qualifiedScopes.Add(new NativeFormatModule.QualifiedScopeDefinition(metadataUnit, scopeHandle)); } return(new NativeFormatModule(Context, qualifiedScopes.ToArray())); }
private static CoreTypeResolver CreateCoreTypeResolver(Func <Assembly, string, bool, Type> typeResolver, IList <string> defaultAssemblyNames, bool throwOnError, bool ignoreCase) { if (typeResolver == null) { return(delegate(Assembly containingAssemblyIfAny, string coreTypeName) { if (containingAssemblyIfAny != null) { return containingAssemblyIfAny.GetTypeCore(coreTypeName, ignoreCase: ignoreCase); } else { foreach (string defaultAssemblyName in defaultAssemblyNames) { RuntimeAssemblyName runtimeAssemblyName = AssemblyNameParser.Parse(defaultAssemblyName); RuntimeAssembly defaultAssembly = RuntimeAssembly.GetRuntimeAssemblyIfExists(runtimeAssemblyName); if (defaultAssembly == null) { continue; } Type resolvedType = defaultAssembly.GetTypeCore(coreTypeName, ignoreCase: ignoreCase); if (resolvedType != null) { return resolvedType; } } if (throwOnError && defaultAssemblyNames.Count > 0) { // Though we don't have to throw a TypeLoadException exception (that's our caller's job), we can throw a more specific exception than he would so just do it. throw Helpers.CreateTypeLoadException(coreTypeName, defaultAssemblyNames[0]); } return null; } }); } else { return(delegate(Assembly containingAssemblyIfAny, string coreTypeName) { string escapedName = coreTypeName.EscapeTypeNameIdentifier(); Type type = typeResolver(containingAssemblyIfAny, escapedName, ignoreCase); return type; }); } }
// // Lex the next segment as the assembly name at the end of an assembly-qualified type name. (Do not use for // assembly names embedded inside generic type arguments.) // // Terminated by NUL. There are no escape characters defined by the typename lexer (however, AssemblyName // does have its own escape rules.) // public RuntimeAssemblyName GetNextAssemblyName() { SkipWhiteSpace(); int src = _index; char[] buffer = new char[_chars.Length]; int dst = 0; for (; ;) { char c = _chars[src]; if (c == NUL) { break; } src++; buffer[dst++] = c; } _index = src; String fullName = new String(buffer, 0, dst); return(AssemblyNameParser.Parse(fullName)); }
public sealed override void InitializeAssemblyName(AssemblyName blank, String fullName) { RuntimeAssemblyName runtimeAssemblyName = AssemblyNameParser.Parse(fullName); runtimeAssemblyName.CopyToAssemblyName(blank); }
public sealed override bool Bind(RuntimeAssemblyName refName, out AssemblyBindResult result, out Exception exception) { bool foundMatch = false; result = default(AssemblyBindResult); exception = null; refName = refName.CanonicalizePublicKeyToken(); // At least one real-world app calls Type.GetType() for "char" using the assembly name "mscorlib". To accomodate this, // we will adopt the desktop CLR rule that anything named "mscorlib" automatically binds to the core assembly. bool useMscorlibNameCompareFunc = false; RuntimeAssemblyName compareRefName = refName; if (refName.Name == "mscorlib") { useMscorlibNameCompareFunc = true; compareRefName = AssemblyNameParser.Parse(AssemblyBinder.DefaultAssemblyNameForGetType); } foreach (KeyValuePair <RuntimeAssemblyName, ScopeDefinitionGroup> group in ScopeGroups) { bool nameMatches; if (useMscorlibNameCompareFunc) { nameMatches = MscorlibAssemblyNameMatches(compareRefName, group.Key); } else { nameMatches = AssemblyNameMatches(refName, group.Key); } if (nameMatches) { if (foundMatch) { exception = new AmbiguousMatchException(); return(false); } foundMatch = true; ScopeDefinitionGroup scopeDefinitionGroup = group.Value; result.Reader = scopeDefinitionGroup.CanonicalScope.Reader; result.ScopeDefinitionHandle = scopeDefinitionGroup.CanonicalScope.Handle; result.OverflowScopes = scopeDefinitionGroup.OverflowScopes; } } BindEcmaAssemblyName(refName, ref result, ref exception, ref foundMatch); if (exception != null) { return(false); } if (!foundMatch) { exception = new IOException(SR.Format(SR.FileNotFound_AssemblyNotFound, refName.FullName)); return(false); } return(true); }
// // Retrieves a type by name. Helper to implement Type.GetType(); // public Type GetType(String typeName, bool throwOnError, bool ignoreCase, IEnumerable <String> defaultAssemblyNames) { if (typeName == null) { throw new ArgumentNullException(); } if (typeName.Length == 0) { if (throwOnError) { throw new TypeLoadException(SR.Arg_TypeLoadNullStr); } else { return(null); } } AssemblyQualifiedTypeName assemblyQualifiedTypeName; try { assemblyQualifiedTypeName = TypeParser.ParseAssemblyQualifiedTypeName(typeName); } catch (ArgumentException) { // Input string was a syntactically invalid type name. if (throwOnError) { throw; } return(null); } if (assemblyQualifiedTypeName.AssemblyName != null) { defaultAssemblyNames = new String[] { null }; } Exception lastTypeLoadException = null; foreach (String assemblyName in defaultAssemblyNames) { RuntimeAssembly defaultAssembly; if (assemblyName == null) { defaultAssembly = null; } else { RuntimeAssemblyName runtimeAssemblyName = AssemblyNameParser.Parse(assemblyName); Exception e = RuntimeAssembly.TryGetRuntimeAssembly(this, runtimeAssemblyName, out defaultAssembly); if (e != null) { continue; // A default assembly such as "System.Runtime" might not "exist" in an app that opts heavily out of pay-for-play metadata. Just go on to the next one. } } RuntimeType result; Exception typeLoadException = assemblyQualifiedTypeName.TryResolve(this, defaultAssembly, ignoreCase, out result); if (typeLoadException == null) { return(result); } lastTypeLoadException = typeLoadException; } if (throwOnError) { if (lastTypeLoadException == null) { throw new TypeLoadException(SR.Format(SR.TypeLoad_TypeNotFoundByGetType, typeName)); } else { throw lastTypeLoadException; } } else { return(null); } }