Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 904213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:00:47+00:00 2026-05-15T16:00:47+00:00

I was trying to setup a unit test for a private inner class, but

  • 0

I was trying to setup a unit test for a private inner class, but had very little success:

namespace Stats.Model
{
  public class DailyStat
  {
    private class DailyStatKey // The one to test
    {
      private DateTime date;
      public DateTime Date 
      { 
        get { return date; }
        set { date = value.Date; }
      }

      public StatType Type { get; set; }

      public override int GetHashCode()
      {
        return Date.Year * 1000000 +
               Date.Month * 10000 +
               Date.Day * 100 +
               (int)Type;
      }

      public override bool Equals(object obj)
      {
        DailyStatKey otherKey = obj as DailyStatKey;
        if (otherKey == null)
          return false;
        return (this.Date == otherKey.Date && this.StatType == otherKey.StatType);
      }
    }
  }
}

I tried this code:

PrivateType statKeyType = new PrivateType("Stats.Model", "Stats.Model.DailyStat.DailyStatKey");

as well as

PrivateType statKeyType = new PrivateType("Stats.Model", "DailyStat.DailyStatKey");

To no avail.

The assembly’s name is “Stats.Model”, and to me the type name looks correct too, but I just get an exception: “System.TypeLoadException: Could not load type”

So what am I doing wrong ?

PrivateType, to the best of my knowledge, is reflection based, and I’d guess it’s pretty much intended for this scenario, as you cannot have a private class directly beneath a namespace.

EDIT:

Added full implementation of DailyStatKey. What I want to test is the uniqueness of my GetHashCode method. As you can see I try to fit a date + type into a single int.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:00:47+00:00Added an answer on May 15, 2026 at 4:00 pm

    Found a solution myself:

    var parentType = typeof(DailyStat);
    var keyType = parentType.GetNestedType("DailyKeyStat", BindingFlags.NonPublic); 
    //edited to use GetNestedType instead of just NestedType
    
    var privateKeyInstance = new PrivateObject(Activator.CreateInstance(keyType, true));
    
    privateKeyInstance.SetProperty("Date", DateTime.Now);
    privateKeyInstance.SetProperty("Type", StatType.Foo);
    
    var hashCode = (int)privateKeyInstance.Invoke("GetHashCode", null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to setup a unit test but whenever I run phpunit -c app
I am trying to build a very simple C++ unit test project. The setup
In trying to setup a unit test for inserting an item into an SQL
I'm trying to create a test class which organizes its test methods using inner
I have the following so far which I am trying to unit test: private
I'm trying to figure out the best way to unit test this class: public
I am trying to write a unit test for the following class: @Transactional public
I'm currently trying to get a unit test set up for an HttpServlet class
I m trying to do a unit test to a class which load and
I'm trying to set up Factory Girl with Test::Unit and Shoulda in Ruby on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.