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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:00:27+00:00 2026-05-13T17:00:27+00:00

I have a generic class that has one type parameter (T). I needed to

  • 0

I have a generic class that has one type parameter (T). I needed to store a collection of these generic objects that are of different types, so I created an interface that the generic class implements as suggested here. There is a property in the generic class of type T that I need to access when iterating through the generic list that contains the collection of Interface objects. So far the only way I have been able to get the value is to call a method using reflection.

interface ISomeClass {

//?
}

class SomeClass<T> : ISomeClass {

 T ValueINeed { get; set;}
}

class ClassThatHasListOfGenericObjects{

 List<ISomeClass> _l = new List<ISomeClass>();

 public AddToList<T>(T someClass) : where T : ISomeClass {

 _l.Add(someClass);

 }

 public SomeMethod(){

   foreach(ISomeClass i in _l){

   i.ValueINeed; //I don't know how to access the property in the generic class

   }
 }
}
  • 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-13T17:00:28+00:00Added an answer on May 13, 2026 at 5:00 pm

    As I see it you have two options. The easy option is to expose the value (as an object) on the interface (and possibly its type as well). Here’s how that would look:

    interface ISomeClass
    {
        object ValueINeed { get; set; }
        // Only needed if you care about static type rather than using ValueINeed.GetType()
        Type TypeOfValue { get; }
    }
    
    class SomeClass<T> : ISomeClass
    {
        public T ValueINeed { get; set; }
        public Type TypeOfValue { get { return typeof(T); } }
    
        object ISomeClass.ValueINeed { get { return ValueINeed; } set { ValueINeed = (T)value; } }
    }
    

    This has the disadvantage that there’s a bit of casting going on and you might need to invoke reflection to do certain things with the value. It has the advantage that it’s easy to understand and implement.

    The other alternative would be to encode an “existential type” which truly represents a SomeClass<T> for some unknown T (like a SomeClass<?> in Java). This is much more complicated and hard to follow, but avoids any casts:

    interface ISomeClassUser<X>
    {
        X Use<T>(SomeClass<T> s);
    }
    
    interface ISomeClassUser
    {
        void Use<T>(SomeClass<T> s);
    }
    
    interface ISomeClass
    {
        X Apply<X>(ISomeClassUser<X> user);
        void Apply(ISomeClassUser user);
    }
    
    class SomeClass<T> : ISomeClass
    {
        public T ValueINeed { get; set; }
    
        public X Apply<X>(ISomeClassUser<X> user) { return user.Use(this); }
        public void Apply(ISomeClassUser user) { user.Use(this); }
    }
    
    // Assumes you want to get a string out, use a different generic type as needed
    class XmlUser : ISomeClassUser<string>
    {
        public string Use<T>(SomeClass<T> s)
        {
            string str = "";
            // do your conditional formatting here, branching on T as needed
            // ...
            return str;
        }
    }
    
    class ClassThatHasListOfGenericObjects
    {
        List<ISomeClass> _l = new List<ISomeClass>();
        XmlUser user = new XmlUser();
    
        public string SomeMethod()
        {
            string s = "";
            foreach (ISomeClass i in _l)
            {
                s += i.Apply(user);
            }
            return s;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Generic Class Factory class that has two methods one utilizes the
I have a class that has a Generic type G In my class model
I have a generic class that takes a type T . Within this class
I have a Generic Base Class that I want to allow one of two
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a Base class that needs a Generic type. That can be either
I have a model that has a unique generic foreign key relationship: class Contact(models.Model):
I have a generic class that needs to limit an enum depending on the
Suppose I have a Generic abstract class that provides some default constructor functionality so
I have a utility class that needs to work on a generic Class but

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.