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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:12:42+00:00 2026-06-18T02:12:42+00:00

I have a factory class that returns concrete instances inheriting from an abstract type.

  • 0

I have a factory class that returns concrete instances inheriting from an abstract type. These concrete instances also have a generic property for their data type, as each instance requires a different data type. The Data property also exists on the abstract type since it is set in the factory.

public static class FactoryClass 
{
  public static TType CreateNewClass<TType>(object data) 
        where TType : AbstractClass<object>, new()
  {
    var newClass = new TType {Data = data};
    // do some stuff
    // keep track of all created types in a list
    List.Add(newClass);
    return newClass;
  }

  private List<AbstractClass<object>> MyList = new List<AbstractClass<object>>()
}

public abstract class AbstractClass<TData>
{
  internal AbstractClass()
  {
    // do constructor things
  }

  protected SomeCommonFunction()
  {
    // common code for abstract base class
  }

  public abstract void DoSomethingToData();

  TData Data;
}

public class ExampleClass : AbstractClass<string[]>
{
  public override void DoSomethingToData()
  {
    // sort the strings or something
    // call the abstract code
  }
}

When I try and call FactoryClass.CreateNewClass<ExampleClass>(myStringArray) I get an error that it needs to be castable to AbstractClass<object>

Is there a better way to do what I’m trying to do?

  • 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-06-18T02:12:43+00:00Added an answer on June 18, 2026 at 2:12 am

    Well this is the problem:

    where TType : AbstractClass<object>
    

    That constraint is exactly the one the compiler is complaining about.

    It sounds to me like you really want something like this:

    public static TContainer CreateNewClass<TContainer, TData>(TData data) 
        where TContainer : AbstractClass<TData>, new()
    

    Unfortunately that then requires you to specify both type arguments to the method – but at least it’s safe in terms of the type of data you’re using. You may be able to work around this by creating two generic methods and a separate class to be used between them, allowing you to write something like:

    FactoryClass.For(myStringArray).CreateNewClass<ExampleClass>();
    

    The For method would return a generic type with a type argument of string[], and then CreateNewClass would be an instance method which took the container type for the data type.

    However, your list simply can’t work in its current form. You may want to create a non-generic base class for AbstractClass:

    public abstract class AbstractClass
    {
        // Anything which doesn't use TData
    }
    

    then:

    public abstract class AbstractClass<TData> : AbstractClass
    

    At that point the list in your factory would be a List<AbstractClass>; you wouldn’t know the data type of each entry in a compile-time-safe way… which makes complete sense as each element could use a different type.

    (Oh, and of course your list field will need to be static if you want to access it from a static method…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have a collection of classes that inherit from an abstract class I created.
I have a factory method that returns the correct sub class depending on three
I have a generic object factory FactoryBase<T> with a factory method: public abstract class
Having in mind the abstract factory pattern, imagine that you have a class hierarchy
In my console application have an abstract Factory class Listener which contains code for
I have a class with a static factory constructor which returns a pointer to
I have a class with a factory-pattern function in it: abstract class ParentObj {
I have a Factory class that creates a Widget object. The Factory object needs
I have SaveManager Abstract class and my concrete classes TVSaveManager, DataSaveManager and VoiceSaveManager implementing

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.