Problems finding classes in namespace and testing extend expected parent

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-04-11T02:37:04Z Indexed on 2010/04/11 2:43 UTC
Read the original article Hit count: 286

Filed under:
|
|
|

So I am in the process of building a site in ASP.Net MVC, and in the process I am adding certain things to my Site.Master I want to make sure that all of my model classes extend a certain base class that contains all of the pieces the Site.Master needs to be operable. I want to test to make sure this assumption isn't broken (I believe this will save me time when I forget about it and can't figure out why a new combination isn't working.)

I wrote a test that I thought would help with this, but I am running into two problems. First it isn't finding the one example model class I have so far in the LINQ call all of a sudden, I am admittedly still a bit new to LINQ. Second, I had it finding the class earlier, but I couldn't get it to verify that the class inherits from the base class. Here is the example test.

[Test]
public void AllModelClassesExtendAbstractViewModel()
{
    var abstractViewModelType = typeof (AbstractViewModel);
    Assembly baseAssembly = Assembly.GetAssembly(abstractViewModelType);
    var modelTypes = baseAssembly.GetTypes()
        .Where(assemblyType => (assemblyType.Namespace.EndsWith("Models")
                               && assemblyType.Name != "AbstractViewModel"))
        .Select(assemblyType => assemblyType);

    foreach (var modelType in modelTypes)
    {
        Assert.That(modelType.IsSubclassOf(abstractViewModelType), Is.True
            , modelType.Name + " does not extend AbstractViewModel");
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ