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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:20:21+00:00 2026-05-26T13:20:21+00:00

From an instance, I might do this. var obj= Activator.CreateInstance(GetType()); Not sure how to

  • 0

From an instance, I might do this.

 var obj= Activator.CreateInstance(GetType());

Not sure how to get typeof of the inherited class in a static base method though.

Is this the best way forward?

 public static Method<T>() where T : SomeBase, new()
  • 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-26T13:20:22+00:00Added an answer on May 26, 2026 at 1:20 pm

    There is no such thing as a derived static method. So there is no way to create a static factory method that returns a different type depending on which derived class you call it on.

    As Lonli-Lokli suggested, you should use the Abstract Factory design pattern.

    public interface ISomething
    {
        void DoSomething();
    }
    
    public class SomeClass : ISomething
    {
        public virtual void DoSomething() { Console.WriteLine("SomeClass"); }
    }
    
    public class SomeDerivedClass : SomeClass
    {
        private int parameter;
    
        public SomeDerivedClass(int parameter)
        {
            this.parameter = parameter;
        }
    
        public virtual void DoSomething()
        {
            Console.WriteLine("SomeDerivedClass - {0}", parameter);
            base.DoSomething();
        }
    }
    
    public interface IFactory
    {
        public ISomething Create();
    }
    
    public class SomeClassFactory : IFactory
    {
        public ISomething Create() { return new SomeClass(); }
    }
    
    public class SomeDerivedClassFactory : IFactory
    {
        public ISomething Create() { return new SomeDerivedClass(SomeParam); }
    
        public int SomeParam { get; set; }
    }
    

    Pros of Abstract Factory vs static Factory methods:

    • It is much more flexible, allowing a new implementation of your factory logic (which can be as complicated as you want) for every implementor of the abstract factory. You could have more than one factory per class, if you wanted.
    • Since you aren’t calling a static method, it is much easier to replace at runtime. This is quite useful for injecting mocks in unit tests.

    The pros are huge. Abstract Factories are superior to static factory methods in every way, even if you could get static methods to work the way you want them to.

    Cons of Abstract Factory vs static Factory methods:

    • Users of the abstract factory must have a factory instance to create your derived types.
    • You have to write a new abstract factory implementation for each derived class.

    The cons are very marginal.

    It is extremely easy for a user to instantiate a factory to create a single object:

    MyClass myClass = new MyClassFactory().Create();
    

    As for code duplication in the factory implementation: Saving the implementer a tiny bit of typing is pointless. It is a goal in programming to write code that can be read, understood, and easily modified. There is no programming goal to save paper or keystrokes 🙂

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

Sidebar

Related Questions

How do you get an instance of the actionscript class Class from an instance
Is there a way to bind properties from one instance of a class to
My ASP.NET application loads an assembly and creates a class instance from an object
I'm not sure if this is possible in Delphi. I've looked around and can't
Ok so this might not be the best title but I am struggling with
Based on this answer Get the subdomain from a URL , I am trying
this question might sound silly to some but i just had to make sure.
from an instance of Site with a ManyToMany relationship to Kiosk, i'd like to
How can I access a controller instance from the view? E.g. I have a
I need to connect to a MongoDB instance from my EJB3 application, running on

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.