Getting a System.Type from Type's Partial Name

How Type.GetType works when given partially qualified type name?

If the DLL it's in isn't already loaded into the application domain (e.g. you used it), you need the full path like this, if it's already loaded, it can find it with the shorter version.

To answer your question: the second version always works, stick with it and you have one way to worry about.

Get Type by Name

Type.GetType can only find types in mscorlib or current assembly when you pass namespace qualified name. To make it work you need "AssemblyQualifiedName".

The assembly-qualified name of the type to get. See
AssemblyQualifiedName. If the type is in the currently executing
assembly or in Mscorlib.dll, it is sufficient to supply the type name
qualified by its namespace.

Referece Type.GetType

System.ServiceModel.NetNamedPipeBinding lives in "System.ServiceModel.dll" hence Type.GetType can't find it.

This will work

Type.GetType(typeof(System.ServiceModel.NetNamedPipeBinding).AssemblyQualifiedName)

Or if you know the assembly already use following code

assemblyOfThatType.GetType(fullName);//This just need namespace.TypeName

typeof: how to get type from string

try
{
// Get the type of a specified class.
Type myType1 = Type.GetType("System.DateTime");
Console.WriteLine("The full name is {myType1.FullName}.");

// Since NoneSuch does not exist in this assembly, GetType throws a TypeLoadException.
Type myType2 = Type.GetType("NoneSuch", true);
Console.WriteLine("The full name is {myType2.FullName}.");
}
catch(TypeLoadException e)
{
Console.WriteLine(e.Message);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}

See Type.GetType(string) on MSDN

How do I determine if System.Type is a custom type or a Framework type?

The only way to safely check if a type is part of an assembly is to check the assembly's fully qualified name which contains its name, version, culture and public key (if signed). All .Net base class libraries (BCL) are signed by microsoft using their private keys. This makes it almost impossible for anyone else to create an assembly with same fully qualified name as a base class library.

//add more .Net BCL names as necessary
var systemNames = new HashSet<string>
{
"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
};

var isSystemType = systemNames.Contains(objToTest.GetType().Assembly.FullName);

A slightly less brittle solution is to use the AssemblyName class and skip the version number/culture check. This of course assumes the public key doesn't change between versions.

//add more .Net BCL names as necessary
var systemNames = new List<AssemblyName>
{
new AssemblyName ("mscorlib, Version=4.0.0.0, Culture=neutral, " +
"PublicKeyToken=b77a5c561934e089"),
new AssemblyName ("System.Core, Version=4.0.0.0, Culture=neutral, "+
"PublicKeyToken=b77a5c561934e089")
};

var obj = GetObjectToTest();

var objAN = new AssemblyName(obj.GetType().Assembly.FullName);

bool isSystemType = systemNames.Any(
n => n.Name == objAN.Name
&& n.GetPublicKeyToken().SequenceEqual(objAN.GetPublicKeyToken()));

Most of the BCL have been signed with the same key but not all. You could use the AssemblyName class to just check the public key token. It depends on your needs.

Getting Class object from its string name in C#

Try using the Assembly Qualified Name instead. If it's running Xamarin, it might need to use the full qualified name. I've had the same limitations with Silverlight.

To quickly get the full name, just reference the type and output it:

Console.WriteLine(typeof(TempIOAddon).AssemblyQualifiedName);

EDIT: Here's the MSDN doc for Type.GetType(string) for Silverlight. Since Xamarin's mobile platform is more or less based off it, generally the docs can apply: http://msdn.microsoft.com/en-us/library/w3f99sx1%28v=vs.95%29.aspx

Note that likely if the type is in the currently executing assembly, you'll probably only need the full namespace.name



Related Topics



Leave a reply



Submit