I have a string such as "Int", "Double", "DateTime" etc. How do I get the fully qualified name from this string?
If you know the assembly's file system path, you can call the static (C#) or Shared (Visual Basic) AssemblyName. GetAssemblyName method to get the fully qualified assembly name.
In C#, the full name of the class starts from its namespace name followed by dot(.) operator and the class name, which is termed as the fully qualified name of the class.
A fully qualified type name consists of an assembly name specification, a namespace specification, and a type name. Type name specifications are used by methods such as Type. GetType, Module. GetType, ModuleBuilder.
GetType(String, Boolean) Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found. public: static Type ^ GetType(System::String ^ typeName, bool throwOnError); C# Copy.
It's worth bearing in mind that int
isn't a type. It's an alias provided by C# for the type System.Int32
. Bearing that in mind, and assuming you only care about core types, you can try:
var typeName = "DateTime";
var systemTypesAssembly = Assembly.Load("mscorlib");
var type = (from t in systemTypesAssembly.GetTypes()
where t.Name == typeName
select t).FirstOrDefault();
Console.WriteLine(type.FullName);
As I said, this will not work for Int
, but would work for Int32
which is the type it's aliased to. This code also contains no error handling for where, for example, no type is found in mscorlib that matches the one you've entered in typeName
Before I go any further, I'd like to point that there is no class in the .NET Framework base class library named Int
. There is System.Int32
, that in C# happens to have an alias: int
.
You can do this several ways.
A simple but limited way to do this is to have a dictionary mapping strings to the corresponding FQNs:
public static IDictionary<string, string> FqnMap = new Dictionary<string, string>
{
{ "Int", "System.Int32" },
{ "Double", "System.Double" },
{ "DateTime", "System.DateTime" },
};
public string GetFqn(string name)
{
return FqnMap[name]; // TODO: add error handling
}
A more complex but powerful way would involve using reflection to search all types from a set of assemblies. Something like this:
public class FqnResolver
{
public FqnResolver(IEnumerable<Assembly> assemblies)
{
Assemblies = new List<Assembly>(assemblies);
}
public FqnResolver(params Assembly[] assemblies) : this(assemblies as IEnumerable<Assembly>) { }
public FqnResolver() : this(new Assembly[0]) { }
public ICollection<Assembly> Assemblies { get; private set; }
public string GetFqn(string name)
{
var candidates = from a in Assemblies
from t in a.GetTypes()
where t.Name == name
select t.FullName;
return candidates.First(); // will throw if no valid type was found
// and does not count duplicates
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With