Skip to content

Instantly share code, notes, and snippets.

@christopherhouse
Created November 5, 2013 19:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save christopherhouse/7325139 to your computer and use it in GitHub Desktop.
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ThirteenDaysAWeek.TypeLoadFromString.Tests
{
[TestClass]
public class TypeLoadTests
{
[TestMethod]
public void GetType_Should_Return_Null_For_FullName()
{
// Arrange
Type theType = typeof(Domain<CustomerModel>);
string fullName = theType.FullName;
// Act
// FullName will have the assembly qualified name for any generic type parameters but *NOT* for the generic type itself.
// This results in loadedType being null when we call Type.GetType because the type we're trying to load is in another
// assembly. By default, Type.GetType searches the current assembly and mscorlib only. Since the generic type isn't
// fully qualified, Type.GetType will return null.
Type loadedType = Type.GetType(fullName);
// Assert
loadedType.Should()
.BeNull();
}
[TestMethod]
public void GetType_Should_Return_Type_Instance_For_AssemblyQualifiedName()
{
// Arrange
Type theType = typeof (Domain<CustomerModel>);
string assemblyQualifiedName = theType.AssemblyQualifiedName;
// Act
// AssemblyQualifiedName will have the assembly qualified type name for both the generic type as well as any type parameters.
// Since the generic type and it's parameter type are located in a separate assembly but are loaded using fully qualified
// names, Type.GetType will return an instance of the specified type.
Type loadedType = Type.GetType(assemblyQualifiedName);
// Assert
loadedType.Should()
.NotBeNull();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment