An overload in Type.GetType accepts a function that can be used to solve AssemblyName for assembly. A return value of null usually throws an exception, because the type cannot be resolved, but this can be suppressed by passing false to the throwOnError parameter.
The function used for resolution can also set a string variable in the outer scope returned by the source code.
using System; using System.Diagnostics; using System.Reflection; namespace ConsoleApp { public static class Program { public static void Main() { var assemblyName = GetAssemblyName("MyNamespace.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Debug.Assert(assemblyName == "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); } public static String GetAssemblyName(String typeName) { String assemblyName = null; Func<AssemblyName, Assembly> assemblyResolver = name => { assemblyName = name.FullName; return null; }; var type = Type.GetType(typeName, assemblyResolver, null, false); if (type != null) return type.AssemblyQualifiedName; return assemblyName; } } }
source share