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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:05:36+00:00 2026-05-29T07:05:36+00:00

I am looking for a c# equivelent of my code for a pluggable factory.

  • 0

I am looking for a c# equivelent of my code for a pluggable factory. The advantage of the linked method is that static initialization result in a push operation, where each plugin adds itself to the factory.

C++ code for a pluggable factory: (http://codepad.org/7pgzaaAK)

// base class for plugins
class Foo{
public:
    virtual std::string getName()const=0;
    virtual void exercise()const=0;
};

// plugin factory
class FooFactory{
    public:
    static Foo* GetA(std::string s);
    typedef std::map<std::string,Foo*(*)(void)> mapType;
    static mapType& getA();
};

FooFactory::mapType& FooFactory::getA(){static mapType getA;return getA;}
Foo* FooFactory::GetA(std::string s)
{return getA().find(s)!=getA().end()?getA()[s]():0;} // to simplify access

// helper function to add the fun
template<typename T>
Foo* getNew(){  return new T; }

// use the CRTP to automatically register the object with the factory.
template <typename T> struct Reg { Reg() { /*cout << "registering a " << T().getName( <<endl;*/
FooFactory::getA().insert(std::pair<std::string, Foo*(*)()>(T().getName(), &getNew<T>));} };

template <typename T>
class Foo_reg:public Foo{
public:
    Foo_reg(){&reg;}; // using a reff to the static is enough to force initialization of the static earlier
    static Reg<T> reg;
};
template <typename T> Reg<T> Foo_reg<T>::reg;

/////////////////
class FooBar:public Foo_reg<FooBar>{ // automatic registration with the factory
public:
    FooBar(){a=10;}
    virtual std::string getName()const{return "Foo Bar";}
    virtual void exercise()const {cout <<a;}
private:
    int a;
};

// exercise the factory and objects.
int main(){
    Foo* foo=FooFactory::GetA("Foo Bar");
    foo->exercise();
}

In C# i can see 2 ways of doing this, both of them pull operations

  1. Have an explicit pre-built list of all plugins, which has to be maintained seperatly from the plugins themselves.

  2. Use code reflection to iterate through all types, checking if they are castable to Foo and initialize their statics, on any dll load and program start.

Is it possible to do this without having to resort to these methods?

  • 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-29T07:05:37+00:00Added an answer on May 29, 2026 at 7:05 am

    I wouldnt worry about reflection to much. Even PEX has no trouble using it. Simply put, by reflection, you’re just checking the metadata of an assembly and see if it defined any classes that implement a certain interface or are marked with a certain attribute, this is very fast! Anyways, the CLR will never run code that is not being called explicitly so no, you have to resort to some kind of pulling mechanism (even if you make it look like a push mechanism)

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

Sidebar

Related Questions

I'm looking for an code example/library or something that to be equivalent to PrintDocument
I've got some code that needs to scan through a hierarchical database schema, looking
I was looking for a method in ConcurrentDictionary that allows me to remove an
I'm looking for an equivalent of: for(_ <- 1 to n) some.code() that would
I'm looking for a code/lib in php that I will call it and pass
I'm looking for a Ruby's equivalent of Code Like a Pythonista: Idiomatic Python Desirable
I'm looking for the Go equivalent of scanf(). I tried with following code: 1
I'm looking for the equivalent in Qt to GetTickCount() Something that will allow me
I'm looking for the equivalent of the Unix 'tail' command that will allow me
I'm looking for an equivalent of PerlCritic for PHP. PerlCritc is a static source

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.