Given that I have a type "Wibble.Wobble" that exists in an assembly "Foo.Bar"
And that assembly is not loaded into the main applications load context
Is there an existing public mechanism for parsing the following string into its subsequent parts:
"Wibble.Wobble, Foo.Bar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xyz"
The reason for asking is that I a need to load a variety of types at runtime, some of which may be in the current load context. Others may need to be loaded from an external assembly.
I know how to load types from the current load context Type.GetType("typename")
, I know how to load an assembly and get a type from it Assembly.LoadFile("xyz").GetType("abc")
. What I am having problems with is that if you try to load a type from an assembly it should not be a fully qualified name where as you can when using Type.GetType
.
The solution itself should not be that complicated, it is just splitting the string before the first comma. However I wanted to know if there was a way of getting the fully qualified name as an object representation so I didn't have to do this and it would help with locating the right assembly without having to loop through a whole directory.
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.
A fully-qualified class name in Java contains the package that the class originated from. An example of this is java. util. ArrayList. The fully-qualified class name can be obtained using the getName() method.
To search for and load a Type, use GetType either with the type name only or with the assembly qualified type name. GetType with the type name only will look for the Type in the caller's assembly and then in the System assembly. GetType with the assembly qualified type name will look for the Type in any assembly.
GetType(String, Boolean, Boolean) Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.
After all my digging around I do not believe there is anything publicly available in the Framework that can do this. I would love for someone to show me different as I prefer to use core functionality that is (or should be) better tested and written by someone who has a better understanding of the framework than I do.
As such I have rolled my own strongly typed TypeName
object.
class TypeName
{
public TypeName(string name)
{
var index = name.LastIndexOf(',');
if (index > 0)
{
Name = name.Substring(0, index).Trim();
AssemblyName = new AssemblyName(name.Substring(index + 1).Trim());
}
else
{
Name = name;
}
}
public string Name { get; private set; }
public AssemblyName AssemblyName { get; private set; }
}
The object takes in a string
which should be either a type name or a fully qualified type name. If it is a fully qualified type name then the AssemblyName attribute is set using the AssemblyName class other wise it is left as null.
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