add CreateType<T> convenience method

This commit is contained in:
Michael Becker 2021-01-09 22:28:28 -05:00
parent 178b6c8913
commit 2fed2cb5ef
No known key found for this signature in database
GPG Key ID: 98C333A81F18C22C

View File

@ -253,5 +253,24 @@ namespace MBS.Framework
}
return list.ToArray();
}
/// <summary>
/// Creates and returns an instance of the type with the specified fully-qualified
/// (or partially-qualified if <paramref name="usingNamespaces"/> is specified) type name.
/// </summary>
/// <returns>An instance of the type with the specified fully-qualified type name.</returns>
/// <param name="typeName">The fully-qualified or partially-qualified type name to search.</param>
/// <param name="usingNamespaces">Array of <see cref="string" /> namespaces with which to prefix
/// <paramref name="typeName" /> during the search, if a type with the fully-qualified
/// <paramref name="typeName" /> is not found.</param>
/// <typeparam name="T">The type of instance to return.</typeparam>
public static T CreateType<T>(string typeName, string[] usingNamespaces = null)
{
Type t = FindType(typeName, usingNamespaces);
if (t == typeof(T) || t.IsSubclassOf(typeof(T)))
return (T)(t.Assembly.CreateInstance(t.FullName));
return default(T);
}
}
}