public Assembly Resolve(AssemblySerializationInfo info) { #warning Cache results var assemblyName = new AssemblyName { Name = info.Name, Version = info.Version }; if (!string.IsNullOrEmpty(info.Token)) { var publicKeyToken = info.Token.ParseAsHexByteArray(); assemblyName.SetPublicKeyToken(publicKeyToken); } return(Assembly.Load(assemblyName)); }
internal static AssemblySerializationInfo ParseInternal(string fullName, int startIndex, int endIndex) { var result = new AssemblySerializationInfo(); // Skip whitespace while (startIndex < endIndex && fullName[startIndex] == ' ') { startIndex++; } // Parse Name var nameEndIndex = startIndex; for (; nameEndIndex < endIndex; nameEndIndex++) { if (fullName[nameEndIndex] == ',' || fullName[nameEndIndex] == ' ') { break; } } result.Name = fullName.Substring(startIndex, nameEndIndex - startIndex); startIndex = nameEndIndex; // Skip whitespace while (startIndex < endIndex && fullName[startIndex] == ' ') { startIndex++; } // Pairs while (startIndex < endIndex && fullName[startIndex] == ',') { startIndex++; // Skip whitespace while (startIndex < endIndex && fullName[startIndex] == ' ') { startIndex++; } var pairEndIndex = fullName.IndexOf(',', startIndex); if (pairEndIndex < 0 || pairEndIndex > endIndex) { pairEndIndex = endIndex; } // Parse Key var keyEndIndex = startIndex; for (; keyEndIndex < pairEndIndex; keyEndIndex++) { if (fullName[keyEndIndex] == '=' || fullName[keyEndIndex] == ' ') { break; } } var key = fullName.Substring(startIndex, keyEndIndex - startIndex); startIndex = keyEndIndex; // Skip whitespace while (startIndex < endIndex && fullName[startIndex] == ' ') { startIndex++; } if (fullName[startIndex] != '=') { throw new InvalidOperationException("Assembly full name format error"); } startIndex++; // Skip whitespace while (startIndex < endIndex && fullName[startIndex] == ' ') { startIndex++; } // Parse Value var valueEndIndex = startIndex; for (; valueEndIndex < pairEndIndex; valueEndIndex++) { if (fullName[valueEndIndex] == ',' || fullName[valueEndIndex] == ' ') { break; } } var value = fullName.Substring(startIndex, valueEndIndex - startIndex); startIndex = valueEndIndex; switch (key) { case "Version": result.Version = Version.Parse(value); break; case "Culture": result.Culture = value; break; case "PublicKeyToken": result.Token = value; break; default: throw new InvalidOperationException("Assembly full name unknown property " + key); } // Skip whitespace while (startIndex < endIndex && fullName[startIndex] == ' ') { startIndex++; } } return(result); }
internal static TypeSerializationInfo ParseInternal(string typeFullName, ref int startIndex) { var result = new TypeSerializationInfo(); // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } // Parse Name var nameEndIndex = startIndex; for ( ; nameEndIndex < typeFullName.Length; nameEndIndex++) { if (typeFullName[nameEndIndex] == '[' || typeFullName[nameEndIndex] == ']' || typeFullName[nameEndIndex] == ',' || typeFullName[nameEndIndex] == ' ') { break; } } result.Name = typeFullName.Substring(startIndex, nameEndIndex - startIndex); startIndex = nameEndIndex; // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } // Generic arguments if (startIndex < typeFullName.Length && typeFullName[startIndex] == '[') { startIndex++; // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } var genericArgs = new List <TypeSerializationInfo>(capacity: 2); var hasMoreGenericArgs = true; while (hasMoreGenericArgs) { bool isTypeNameQuoted = false; if (typeFullName[startIndex] == '[') { isTypeNameQuoted = true; startIndex++; } // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } var genericArg = ParseInternal(typeFullName, ref startIndex); genericArgs.Add(genericArg); // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } if (isTypeNameQuoted) { if (typeFullName[startIndex] != ']') { throw new InvalidOperationException("Type full name format error"); } startIndex++; // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } } if (typeFullName[startIndex] == ']') { hasMoreGenericArgs = false; startIndex++; // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } } else if (typeFullName[startIndex] == ',') { startIndex++; // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } } else { throw new InvalidOperationException("Type full name format error"); } } result.GenericArgs = genericArgs.ToArray(); } // Assembly name if (startIndex < typeFullName.Length && typeFullName[startIndex] == ',') { startIndex++; // Skip whitespace while (startIndex < typeFullName.Length && typeFullName[startIndex] == ' ') { startIndex++; } var endIndex = typeFullName.IndexOf(']', startIndex); if (endIndex < 0) { endIndex = typeFullName.Length; } result.Assembly = AssemblySerializationInfo.ParseInternal(typeFullName, startIndex, endIndex); startIndex = endIndex; } return(result); }