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

  • Home
  • SEARCH
  • 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 587187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:11:11+00:00 2026-05-13T15:11:11+00:00

I have a class hierachy like this public class A { protected class B

  • 0

I have a class hierachy like this

public class A
{
    protected class B 
    {
        String Name { get; set; }
    }

    protected class C : KeyedCollection<String, B> 
    {
        // ...
    }

    protected C Collection { get; }

    // ...

    public A Copy () 
    {
        // Creates a deep copy of this instance.
    }
}

Now I’d like to write a unit test to compare if two instances of A have the same items B inside the property KeyedCollection. However, I’m not being able to perform a foreach loop into the A instances. What I had tried,

[TestClass]
public class TestClass
{
    public void ATest()
    {
        A original = new A();
        A copy = A.Copy();

        // ...

        A_Accessor originalAccessor = A_Accessor.AttachShadow(original);
        A_Accessor copyAccessor = A_Accessor.AttachShadow(copy);

        foreach(var originalItem in originalAccessor.Collection)
        {
            var copyItem = copyAccessor[originalItem.Name];
            Assert.AreEqual(originalItem, copyItem);
        }
    }
}

This code doesn’t even compile because the C class accessor doesn’t implements the IEnumerable interface (it doesn’t implement any interface from KeyedCollection class). Does anyone have an idea about how can I overcome this issue?

The error message I’m getting is

foreach statement cannot operate on variables of type ‘C’ because ‘A_Accessor.C’ does not contain a public definition for ‘GetEnumerator’

  • 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-13T15:11:11+00:00Added an answer on May 13, 2026 at 3:11 pm

    I just tried to compile your example: As expected I got an Error

    Inconsistent accessibility: field type 'A.C' is less accessible than field 'A.Collection'.

    Basically that means that you cannot declare a protected property using a private type. So it’s not a problem with your test code but with the code to be tested …

    EDIT

    You could use originalAccessor.Collection.Target and cast it to ICollection. Of course you can only enumerate over objects in this case, so you’ll have to cast each item again:

    foreach (var item in (originalAccessor.Collection.Target as ICollection)) {
       A_Accessor.B casted = A_Accessor.B.AttachShadow(item);
       var copyItem = copyAccessor[casted.Name];
       Assert.AreEqual(casted, copyItem);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class hierarchy, this one: type TMatrix = class protected //... public
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
Here's the deal. I have a big class hierarchy and I have this one
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have a class hierarchy as such: +-- VirtualNode | INode --+ +-- SiteNode
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class with a member function that takes a default argument. struct Class

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.