In later versions (insert version number here) of the framework, using GetType()
and giving it a type string with the wrong version number will still give you the correct type instead of null.
If you write a library that can be used by applications with a previous version (insert version number here), you can instead give to the GetType()
Method a string that does not include the version instead.
Remember that asking the fullName
of a generic type will give you a string including the versions of the generic type parameters, you will need to write the type string yourself. You can use my code for this, it uses @Lars's code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hlwSerial
{
public static class TypeHelper
{
/// <summary>
/// Gives a shortened assemblyQualifiedName of the type, with only its fullName and the name of its assembly. Does the same for its generic type parameters if it has any.
/// </summary>
/// <param name="type">The type of which you'll get the name</param>
/// <param name="inBrackets">default to false. Put true if the result should be surrounded by brackets in the case of being a generic type parameter.You shouldn't have to set it to true yourself.</param>
/// <returns></returns>
public static string GetShortTypeName(this Type type, bool inBrackets = false)
{
if (type.IsGenericType) return type.GetShortGenericName(inBrackets);
if (inBrackets) return $"[{type.FullName}, {type.Assembly.GetName().Name}]";
return $"{type.FullName}, {type.Assembly.GetName().Name}";
}
/// <summary>
/// Private function that will be called by the GetShortTypeName method if the type tested is generic.
/// </summary>
/// <param name="type">The type of which you'll get the name</param>
/// <param name="inBrackets">default to false. Put true if the result should be surrounded by brackets in the case of being a generic type parameter. You shouldn't have to use this.</param>
/// <returns></returns>
private static string GetShortGenericName(this Type type, bool inBrackets = false)
{
if (inBrackets)
return $"[{type.GetGenericTypeDefinition().FullName}[{string.Join(", ", type.GenericTypeArguments.Select(a => a.GetShortTypeName(true)))}], {type.Assembly.GetName().Name}]";
else
return $"{type.GetGenericTypeDefinition().FullName}[{string.Join(", ",type.GenericTypeArguments.Select(a=> a.GetShortTypeName(true)))}], {type.Assembly.GetName().Name}";
}
}
}