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 1898178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:44:04+00:00 2026-05-17T06:44:04+00:00

I have three classes: SomeThing, SomeOtherThing, and YetAntherThing. All three have an identical member

  • 0

I have three classes: SomeThing, SomeOtherThing, and YetAntherThing. All three have an identical member called Properties. In each class, it is a key/value pair such that I can reference obj1.Name, obj1.Value, obj2.Name, obj2.Value, obj3.Name, and obj3.Value. I’d like to pass these three objects into a single method that could iterate through their respective “Properties” collections without having to know at compile time which it was operating on. I envision something like:

SomeThing obj1;
SomeOtherThing obj2;
YetAntherThing obj3;

DoProperties( obj1, obj1.GetType() );
DoProperties( obj2, obj2.GetType() );
DoProperties( obj3, obj3.GetType() );

...

private void DoProperties( object obj, Type objectType )
{
    // this is where I get lost. I want to "cast" 'obj' to the type
    // held in 'objectType' so that I can do something like:
    //
    // foreach ( var prop in obj.Properties )
    // {
    //    string name = prop.Name;
    //    string value = prop.Value;
    // }
}

Note: The classes SomeThing, SomeOtherThing, and YetAntherThing are defined externally, I have no control over them or access to their source code, and they are all sealed.

  • 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-17T06:44:04+00:00Added an answer on May 17, 2026 at 6:44 am

    You’ve got two options; either get each class to implement an interface that exposes the collection, eg:

    interface IHasProperties
    {
        PropertyCollection Properties {get;}
    }
    

    Then declare your method, referencing that interface:

    private void DoProperties(IHasProperties obj)
    {
        foreach (var prop in obj.Properties)
        {
            string name = prop.Name;
            string value = prop.Value;
        }
    }
    

    Or use reflection to look-up the Properties collection at run-time, e.g.:

    private void DoProperties(object obj)
    {
        Type objectType = obj.GetType();
    
        var propertyInfo = objectType.GetProperty("Properties", typeof(PropertyCollection));
    
        PropertyCollection properties = (PropertyCollection)propertyInfo.GetValue(obj, null);
    
        foreach (var prop in properties)
        {
            //    string name = prop.Name;
            //    string value = prop.Value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three classes that all have a static function called 'create'. I would
I have three classes; Classes A and B both reference class C . How
Say I have three classes: class X{}; class Y{}; class Both : public X,
If I have three classes class A class B extends A class C extends
I have three classes like this. class A { public class innerB { //Do
I have multiple classes, all of which I want to send an identical message
I have three domain classes User , Employee and UserEmployee . class User {
Say I have three window classes, one for each OS I want to support:
I have a file different_classes that contains three different classes. It is something like:
I have three classes like this : Holiday : package mnm class Holiday {

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.