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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:10:09+00:00 2026-05-23T22:10:09+00:00

I’m not sure how to go about this… I have a class that performs

  • 0

I’m not sure how to go about this…

I have a class that performs various functions:

public abstract class EntityBase { ... }

public interface ISomeInterface<T> where T : EntityBase, new() { ... }

public class SomeClass<T> : ISomeInterface<T> { ... }

I’m trying to have a cache of these in a non-generic class:

public class MyClass
{
    //Not sure how to do this
    private ConcurrentDictionary<?, ISomeInterface<?>> Cache { get; set; }
}

The problem is that EntityBase is abstract and can not do new(), The ISomeInterface expects a class that is based off EntityBase and does new(). Is there any way to do what I’m wanting to do?


Update: I realized that for the TKey I can use Type, but I still am not sure what to put for the ISomeInterface.

  • 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-23T22:10:09+00:00Added an answer on May 23, 2026 at 10:10 pm

    Firstly, I think you meant to use ConcurrentDictionary since you have a key/value pair.

    Secondly, you can do it similar to ICloneable. ICloneable has a method that returns an Object instead of a T.

    private ConcurrentDictionary<Type, Object> Cache { get; set; }
    

    Since it’s private you can manage it internally and cast it as needed. Obviously you’ll have to make sure any function calls have a typeparam definition, for example:

    //I omitted error checking on all the examples.
    public class Foo : EntityBase { ... }
    
    void DoSomething<Foo>()
    {
        var someClass = Cache[typeof(Foo)] as ISomeInterface<Foo>
        someClass.Bar();
    }
    

    Also, even if the property had another modifier (public, internal, protected) you could add comments to callers that the Object is a generic type of ISomeInterface where T is a EntityBase, new(). Then, they simply have to cast however they need to.


    You can also have generic methods on the non-generic class to get the cached item as a generic type:

    ISomeInterface<T> GetInstance<T>()
        where T : EntityBase, new()
    {
        return Cache[typeof(T)] as ISomeInterface<T>;
    }
    
    void AddInstance<T>(ISomeInterface<T> instance)
        where T : EntityBase, new()
    {
        Cache[typeof(T)] = instance;
    }
    
    void DoSomething<T>()
    {
        var someClass = GetInstance<T>();
        someClass.Bar();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.